You may want to make the images smaller while testing. On the lab machines, the command to shrink an image by half is:
$ convert myImage.jpg -scale 50% mySmallImage.ppm
If you are interested in using ImageMagick on your own machine, you can download it at the ImageMagick homepage.
def putPixmap( destination, source, x, y, alpha ): # for each row i # for each column j # assign to (r1, g1, b1) the rgb values from source location (j, i) # assign to (r2, g2, b2) the rgb values from destination location (j + x, i + y) # assign to newRed the alpha blend of r1 and r2 # assign to newGreen the alpha blend of g1 and g2 # assign to newBlue the alpha blend of b1 and b2 # set destination pixel (j+x, i+y) to (newRed, newGreen, newBlue)
If you have two colors, (r1, g1, b1) and (r2, g2, b2), then the alpha blend of the two colors is given by:
(newRed, newGreen, newBlue) = (r1*alpha + (1.0-alpha)*r2, g1*alpha + (1.0-alpha)*g2, b1*alpha + (1.0-alpha)*b2)
When you are finished, download the file testpixmap.py 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.
On the lab machines, use the program xv to look at ppm images.
$ xv tpixmap.ppm
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.
What is the essential information that defines a collage?
What is a data structure we can use to hold a mixture of information?
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 = [ [ 'maine1.ppm', 0, 0, 'rbswap', 0.8, False, None ], [ 'maine5.ppm', 200, 150, 'original', 0.8, False, None ] ]
# reads in the files in the collage and stores the pixmaps in the list def readImages( clist ): # for each item in clist # assign to the variable filename the first element in item # assign to the variable src, the pixmap returned by reading filename # assign to the last element of item, 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.
def getImageSize( clist ): # assign to the variable rows, 0 # assign to the variable cols, 0 # for each item in clist # assign to x0 the x offset information in item # assign to y0 the y offset information in item # assign to src the Pixmap reference (last element in item) # 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 rows and cols in it
Test out your getImageSize function using the simple collage list above, after reading in the images using your readImages function. The answer should be 600 wide (cols) and 450 high (rows)
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.