CS 151: Lab #5

Lab Exercise 5: Lists and files

Main course page

The purpose of this lab is to get practice working with lists and files. Lists are extremely useful structures in python for holding sequential data. Files help us to store information and quickly read it into a program.


  1. On the Desktop, make a folder called lab5.
  2. Copy one or more pictures from your network drive lab 4 folder into your lab5 folder. Try to pick two pictures that are not the same size. If you wish, you can use the images below for the lab.


  3. Open up JES. Create a new python file (e.g. lab5.py) and save it in your lab5 folder. Put your name at the top of the file in a comment.
  4. Our goal this week is to define the layout and look of a collage in a text file. Then we write functions to read in the text file, generate the collage, and save the final image to a file. One of our sub-goals is to make this process as efficient as possible in terms of the amount of code we write. We want to avoid duplication and make use of for loops and lists as much as possible.
  5. One of our sub-goals this week is to make a single function imageCopyInto() that takes a list and a background image as arguments, where the list is a set of parameters that specify the foreground image and how it should be placed into the background. We'll make a simple version in lab today, and you can extend its functionality for the assignment.

    Because we are using a list to pass in all of the foreground image information, we first have to decide what information should be there and the order of the list. We're going to include the following information in this order.

    1. Image filename
    2. Image effect
    3. Whether to remove the bluescreen pixels
    4. The x-offset of the foreground image
    5. The y-offset of the foreground image
    6. The alpha value to use when blending the foreground and background images
    7. Whether to mirror the image
    8. The width of a matte for the image (0 for no matte)
    9. The picture object

    Note that these are not all the same kind of data. Some are strings (filename and effect), one is a Boolean value (bluescreen removal), three are integers (x-offset, y-offset, and matte width), one is a floating point value (alpha), and one is a picture object. It is important that we always keep them in the same order within the list, since we'll use indices to select which value we need.

  6. The pseudo-code for the imageCopyInto function is as follows.
    def imageCopyInto( imageInfo, bkg ):
      # for each pixel in the fg image
        # grab the rgb values of the fg pixel
        # calculate whether the pixel is a bluescreen pixel
        # if the pixel is bluescreen AND we want to remove them (imageInfo[2])
          # continue to the next pixel (do nothing)
        # grab the corresponding background pixel (mirroring and matting modifies this)
        # uses imageInfo[3] (xpos) ad imageInfo[4] (ypos)
        # if the effect (imageInfo[1]) is normal
          # calculate the new RGB values
        # if the effect is swap
          # calculate the new RGB values
        # if the effect is negative
          # calculate the new RGB values
        # modify the new values based on the alpha value (imageInfo[5])
        # set the color of the background pixel to the calculated values

    Go ahead and write up this function now.

  7. Let's test out this function by making a main function that builds the list, creates a background image, and calls imageCopyInto. Here is the pseudo-code.
    def main():
      # have the user pick a file
      # build the list with some nominal values for each place
      # read in the image and append it to the list (last place)
      # create a good size background image
      # call imageCopyInto with the list and the background
      # show the bkg
  8. Now we're going to build the other new piece, which is reading the image information from a file. First, open a text editor and write in two lines with reasonable values for each of the parameters for imageCopyInto, separated by commas. For example, the first line below assumes the picture is called pic1.jpg, and says to copy it into the background in the upper left corner, leaving blue screen pixels and implementing a negative effect with a 1.0 alpha value. The second line says put the same picture slightly down and to the right, using a swap effect, removing blue screen pixels, and giving it 50% transparency.
    pic1.jpg, negative, False, 0, 0, 1.0, False, 0
    pic1.jpg, swap, True, 50, 50, 0.5, False, 0
    Save the file into your lab5 directory.
  9. Create a function collageReadFile(), which takes a filename as a parameter and returns a list of lists. Each individual list will be information about a single image. The pseudo-code for the function is as follows.
    def collageReadFile( filename ):
        # open the file
        # read all the lines of the file into a list of lists
        # close the file
        # create an empty list collageInfo
        # for each line in the data read from the file
            # process the information in the line
    	# if there was no information, continue
    	# append the information into collageInfo
        # return collageInfo

    We're going to decompose this problem by writing a separate function to process the information on a line.

  10. To process the information on a line, we'll write a function imageReadInfo( line ), which takes in a string and converts it to a list with all the appropriate values for imageCopyInto.
    def imageReadInformation( line ):
        # split the line on commas and save the result
        # create an empty list
        # the first string is the filename
        # the second string is the effect
        # the third string need to become a Boolean (bluescreen)
        # the fourth and fifth values are coordinates
        # the sixth value is a alpha channel
        # the seventh value is whether to mirror the image
        # the eighth value is the matte width
        # return the list
  11. Finally, we need to create a new main program, main2(), to make use of the new functionality.
    def main2():
        # let the user pick the collage file
        # call collageReadFile and store the return value in a variable collage
        # for each list in collage
            # make a picture given the filename and append the picture to the list
        # make a big background image
        # for each list in collage
            # call imageCopyInto with the list and background
        # show the final background image

    Try out your main2() program. Try editing your file and see what happens. Add more images to the mix.

When you are done with the lab exercises, go ahead and get started on the assignment.