CS 151: Lab 5

Lab Exercise 5: Lists as Information Collections

Main course page

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.


Tasks

  1. Download two or more of the following images. Right click on the link and select 'Save As...'.

    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

  2. On your personal volume, make a folder for project 5 (Project5). Then download the graphics and display packages. You can also just copy them over from project 4.
  3. Copy your filter.py file from your Project4 directory into your Project5 directory.
  4. Now we're going to add alpha blending to the placePixmap function in your filter.py file. This is a modification of your existing function. The new algorithm is given below. Each comment is a line of python code. Note the added function parameter alpha.
    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

    xv tpixmap.ppm

    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.

  5. 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.

    • 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, None ],
            [ 'maine5.ppm', 200, 150, 'original', 0.8, None ] 
            ]
    
  6. Create a new file called collage.py. Given the collage list structure we developed above, 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 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.

  7. Given the collage list structure we developed, write a function that calculates how big the program 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 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. 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. And this is precisely what you will do in the project.

When you are done with the lab exercises, you may begin the project.