Due: Tuesday, March 7, 2017, 11:59 pm
The purpose of this lab time is to prepare for project 5, which will use lists of information to produce collages.
Here is the reference guide to the Zelle graphics package.
You may want to make the images smaller while testing. The command to shrink an image by half is below.
convert myImage.ppm -scale 50% mySmallImage.ppm
def placePixmap( dst, src, x, y, alpha ): # for each row i # for each column j # assign to (r1, g1, b1) the rgb values from src location (j, i) # assign to (r2, g2, b2) the rgb values from dst location (j + x, i + y) # assign to rnew the alpha blend of r1 and r2 # assign to gnew the alpha blend of g1 and g2 # assign to bnew the alpha blend of b1 and b2 # set dst pixel (j+x, i+y) to (rnew, gnew, bnew)
If you have two colors, (r1, g1, b1) and (r2, g2, b2), then the alpha blend of the two colors is given by
(rnew, gnew, bnew) = (r1*alpha + (1.0-alpha)*r2, g1*alpha +
(1.0-alpha)*g2, b1*alpha + (1.0-alpha)*b2)
Remember to remove our comments once you have written the code (they contribute to clutter).
When you are finished, download the file testpixmap.py, read through the test file, and run it. As the usage statement indicates, it wants the names of two ppm images. When the program is finished, look at the image tpixmap.ppm.
If you wish, use the program xv to look at ppm images. Just type
on the command-line and it will display the image. Type 'q' in the image window to make the program quit.
Note that in a terminal you can put a process into the background by adding the & symbol after the command. So if you type
xv tpixmap.ppm &
you can still use the terminal and the xv program will run in the background. If you type return or enter in the xv window it will re-read the image from the file, so you don't have to quit xv while you are experimenting with an image.
The goal of the project this week is to enable you to easily blend together multiple images, with various effects, into a collage. Rather than hard code all the collage positions, however, we want to put the essential information defining a collage into a list. Then we can write a general function that takes in all of that information and generates the correct collage image. To change the collage, we then just change the information defining the collage, not the code.
What is the essential information that defines a collage?
First, we should identify the essential information for a single image, since a collage is just a collection of single images.
To store the information for a single image, we can use a list. To store the information for all of the images, we can use a list of lists. Consider the assignment below, which creates a list that defines a collage with two images.
collageList = [ [ 'cupcakes.ppm', 0, 0, 'rbswap', 0.8, None ], [ 'FallFoliage.ppm', 200, 150, 'original', 0.8, None ] ]
The one problem with using a list is that when we need to access a parameter for a given image, we have to remember that parameter's index in the list. Consider looping over collageList and print out the filename.
for pictureParams in collageList: print pictureParams
We have to remember that the filename will be pictureParams, since we set up the parameter list with that value in the first position.
Using a design where you have to use actual numbers means it is easy to make mistakes when you have to remember whether a given value is in position 4 or position 5. It also makes it hard to modify your code. For example, if you feel you need to add a new parameter to the list of parameters for a picture, what happens if you put it first in the list? That would mean you would have to go through all of your code and change the indexes everywhere you were accessing a parameter value. That's a lot of code and a lot of opportunities to make mistakes.
Instead, how could we use software engineering principles to avoid having to remember the index? What about using a module-level (top-level) variable whose value remains constant throughout the simulation? We can use it to index into the list. For example, we could create a variable IDXFilename that is set to 0 -- the index of the filename in the list of parameters for an image. Then, when we need to access the filename, we use code like
for pictureParams in collageList: print pictureParams[IDXFilename]
instead of the code above. It's a lot easier to remember IDXFilename, and if you decide to change the position of IDXFilename, you have to change only one place in your code.
Create a new file called collage.py. You will want to import filter so you can use your filters to modify the images.
After the import statements, create the module-level variables you will use to access the individual picture lists. Create one variable for each of the picture parameters: filename, X offset, Y offset, filter, alpha, and Pixmap. When naming your variables, please begin each name with IDX to indicate the value is an index, then use a name that indicates the field. Using IDX is not a Python rule, but a convention the differentiates these variables as having a special purpose. Each of the assignments will be something like the following.
IDXFilename = 0
Copy the following test function into your collage.py and then add more print statements to test each of the parameter entries. Run it and make sure it prints out the right information for each of the pictures.
# project test function def test( argv ): collageList = [ [ 'cupcakes.ppm', 0, 0, 'rbswap', 0.8, None ], [ 'FallFoliage.ppm', 200, 150, 'original', 0.8, None ] ] # test that the parameters are accessing the right value for picParams in collageList: print '----------------------' print 'filename:', picParams[IDXFilename] print 'X Offset:', picParams[IDXXoffset] print 'Y Offset:', picParams[IDXYoffset] # add more print statements here, one for each parameter return if __name__ == "__main__": test( sys.argv )
# reads in the files in the collage and stores the pixmaps in the list def readImages( clist ): # for each pictureParams in clist # assign to the variable filename the IDXFilename element of pictureParams # assign to the variable src, the pixmap returned by reading filename (graphics.Pixmap) # assign to the IDXPixmap element of pictureParams the value in src
Test the readImages function by creating a test function in collage.py that makes the above collageList variable, calls readImages, and then prints out the names and sizes of each image. Since in the readImages function you stored a Pixmap in the last location of each picture list, you can use the Pixmap to getWidgth() and getHeight(). You can use the template below in your test function.
# for each picParams in collageList: # print the IDXFilename element of picParams # print the result of using the IDXPixmap element of picParams to call the method getWidth() # print the result of using the IDXPixmap element of picParams to call the method getHeight()
def getImageSize( clist ): # assign to the variable rows, 0 # assign to the variable cols, 0 # for each pictureParams in clist # assign to x0 the x offset information in pictureParams # assign to y0 the y offset information in pictureParams # assign to src the Pixmap reference # assign to dx the offset x0 plus the width of src # if dx is greater than cols # assign to cols, dx # assign to dy the offset y0 plus the height of src # if dy is greater than rows # assign to rows, dy # return a tuple with cols and rows in it
Test out your getImageSize function using the simple collage list above, after reading in the images using your readImages function. You can add the following template to your test code.
# assign to (cols, rows) the result of calling getImageSize with collageList as the argument # print cols, rows
If your getImageSize function worked correctly, then it should print out the width of the second image plus the X offset of the second image and then print out the height of the second image plus the Y offset of the second image.
Now that you have practice working with the collage information lists, you should be able to write a generic buildCollage program that creates a collage from the information in the list. And this is precisely what you will do in the project.
When you are done with the lab exercises, you may begin Project 5.
© 2017 Ying Li. Page last modified: .