Lab Exercise 5: Lists as Information Collections
Download two or more of the following images.
You may want to make the images smaller while testing. The command to shrink an image by half is below.
convert myImage.jpg -scale 50% mySmallImage.ppm
- On the Desktop, make a folder called lab5. Then download the graphics and display packages.
Open a text editor (e.g TextWrangler). Create a new file called
effects.py. Put a comment at the top of the file with your name and the
date. Save it in the lab5 folder.
After the comment lines, write the command to tell python to import the graphics package.
Copy at least 2-3 of your effect functions, like the rgswap we did in lab last week into your effects.py file. Save the file.
- Make a new file called collage.py, put the usual comments at the top and import the graphics and sys packages. Copy your putPixmap function from last week into the file.
Now we're going to add alpha blending to the putPixmap function. The
algorithm is given below.
def putPixmap( dst, src, x, y, alpha ): # for each row i # for each column j # get the rgb values from src location (j, i) # get the rgb values from dst location (j + x, i + y) # set dst pixel (j+x, i+y) to the alpha blend of the two colors
If you have two colors, (r1, g1, b1) and (r2, g2, b2), then the alpha blend of the two colors is given by
(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.
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?
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.
- Filename of the image
- X offset in the collage
- Y offset in the collage
- Effect for the image
- Alpha blend value
- The Pixmap object (after being read in)
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 ] ]
Given the collage list structure we developed together, write a
function that reads in all of the images in the collage and stores
them in the collage list.
# 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 this in a main function by calling readImages with your collage list, then looping over the collage list and displaying each image using the display.displayPixmap() function.
Given the collage list structure we developed, write a function that calculates
how big it needs to make the background Pixmap to hold the collage.
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.
Once you have finished the lab, go ahead and get started on project 5.