Lists as Information Collections

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

  2. On your personal volume, make a folder for project 5. Then copy over the graphics and display packages from project 4.
  3. Copy your filter.py file from your project 4 directory into your project 5 directory.
  4. Now we're going to add alpha blending to the putPixmap 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.
    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.

  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?

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