CS 151: Lab #6

Lab Exercise 6: Lists and movies

Main course page

The purpose of this lab is to give you more practice with lists and algorithm design. First, we're going to write some helper functions for collages. Second, we're going to build some animated versions of them.

Tasks

  1. On the Desktop, make a folder called lab6. Copy your python file from lab5 into your lab6 directory and rename it collage.py. The file should contain the following functions.
    • collageReadFile() - reads collage information from a text file and returns the collage information as a list of image lists.
    • collageReadImages( collage ) - reads the picture data using the filenames stored in the collage (list of image lists) and stores the picture data in the last element of each of the image lists.
    • collageBuildImage( collage ) - creates a blank background image and places each image in the collage (list of images lists) into the background. The function then returns the background image.
    • imageCopyInto( imagelist, bkg ) - inserts the image described by imagelist into the bkg image using the parameters in the image list.

    Delete your main function from the file and make sure there is no extraneous code in the file that is not inside a function block.

    Finally, at the top of the collage.py file, put the line:

    from media import *

    which imports the JES media functions into your file.

    If you are not happy with your own functions, for the lab exercise you may use my collage.py file.

  2. You'll need some images for the lab today. If you wish, you can use the images below for the lab. These are nice small versions of the images.

    pic1.jpg
    pic2.jpg
    pic3.jpg
    pic4.jpg
    pic5.jpg

  3. Open up JES. Create a new python file (e.g. lab6.py) and save it in your lab6 folder. Put your name at the top of the file in a comment.
  4. First, let's write a helper function that prints out the information in a collage (a list of image lists) in a nice way.
    def collagePrint( thing ):
    
      # print out an opening square bracket
      # for each image in the collage (thing)
        # print out all but the last element of the list image[:-1]
      # print out a closing square bracket
    

    Go ahead and write this function

  5. Second, let's write a helper function that calculates how big the background image has to be in order to hold all of the elements in the collage. The function should return a 2-element list that holds the width and height of the collage.
    def collageSize( thing ):
      
      # initialize two local variables, maxw, and maxh to the value 0
      # for each image in thing
        # calculate the width as the image width + xoffset
        # calculate the height as the image height + yoffset
        # if the width is greater than maxw
          # set maxw to width
        # if the height is greater than maxh
          # set maxh to height
    
      # return a list with maxw and maxh as the two elements
    

    Go ahead and write this function

  6. Now we're going to create a main program that reads in a collage information file and then creates a sequence of collages where the alpha values of the images go from 0 in the first collage to 1.0 in the final collage. As we create each collage we'll write it to a file.

    First, to make our lives easier, we're going to write a quick function called imageSetAlpha that takes an image as the first argument and a new alpha value as the second argument and sets the proper field. Then we don't have to remember which field is alpha any more. This kind of a function is called an accessor function, and you may want to write variations of this function for other image parameters.

    Second, we will need to access the functions in collage.py. There are two things we have to do in order to import collage.py into our program. One is to tell JES where to look for the file. At the top of your lab6.py, put a call to setLibPath() with the argument being a string that is the path to your working directory. You can get the string by typing pwd in a Terminal when you are in the proper directory. It will probably look something like the following, substituting your user name for mine.:

    setLibPath( '/Users/bmaxwell/Desktop/lab6' )

    Note: if you starting working on a different machine in a different directory, you will need to change this to match. A smart thing to do may be to create a variable mypath that holds the path string. It turns out we will need the path again elsewhere in the program.

    The second thing we need to do is import the functions in collage.py. Use an import statement to do that.

    from collage import *

    Now, we can describe the process in the main function.

    def main1():
    
      # read in the collage information file using the collageReadFile() function
      # use the collagePrint() function to print it out
      # read in the image data using the collageReadImages() function
      # create a local variable that holds the number of frames we're going to build
      # for i in range(the number of frames)
        # for each image in the collage
          # use imageSetAlpha to set the alpha value to i / (NumFrames-1)
        # use collageBuildImage() to generate the collage
        # use writePictureTo() to write the image to a file
    

    Write this main function and then try running it.

  7. The final step in lab is to duplicate the main1() function and, instead of writing only a set of images, make a movie. First, copy main1(), paste it, and make it into main2(). The pseudo-code is not much different from before. Before you run the program, create a directory in your lab6 directory called movie.
    def main2():
    
      # read in the collage information file using the collageReadFile() function
      # use the collagePrint() function to print it out
      # read in the image data using the collageReadImages() function
      # create a local variable that holds the number of frames we're going to build
      # for i in range(the number of frames)
        # for each image in the collage
          # use imageSetAlpha to set the alpha value to i / NumFrames
        # use collageBuildImage() to generate the collage
        # make a filename for the movie that is 'movie/' + filename
        # use writePictureTo() to write the image to a file using the filename
    
      # call the function makeMovieFromInitialFile( pickAFile() )
      #    (pick the first image file in the movie subdirectory)
      # call the function writeQuicktime() to build the movie, using 'movie.mov' as path
    

    Make the modifications to main2 and then try running it.

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

Appendix

Dynamically created strings: we often want to make strings that are combinations of variables and static string elements. When building the strings, sometimes we want more control over the appearance of the dynamic elements than is affored by the just converted it to a string. Python gives us this ability using format strings.

python documentation

For example, if we want to put an integer from the variable i into a string with 4 digits and pack it with leading zeros, we could use the format string and expression:

"%04d" % (i)

To build a filename, you combine the static and dynamic parts of the string.

filename = "frame%04d.jpg" % (i)

Try this out on the command line in a simple for loop to see what happens.

for i in range(5):
    filename = "frame%04d.jpg" % (i)
    print filename