CS 151: Lab #4
n

Lab Exercise 4: Images, Conditionals, and Pixels

Main course page

The purpose of this lab is to get you thinking about hierarchical design of programs, give you practice with conditionals and to introduce you to reading information from a file. For the assignment you'll make a collage out of multiple images.

Tasks

  1. On the Desktop, make a folder called lab4.
  2. Grab a copy of your picture from here and save it to your lab4 folder.
  3. Make a smaller copy of your picture. The way to do this in a Terminal is to open up Terminal, cd into your lab4 folder, then execute the following command, substituting the name of your image for myImage.jpg.

    convert myImage.jpg -scale 25% small.jpg

  4. Open up JES. Create a new python file (e.g. lab4.py) and save it in your lab4 folder. Put your name at the top of the file in a comment.
  5. The first task in lab will be to create a function that acts like the JES copyInto function, but that copies only the foregound pixels, leaving out the bluescreen. The algorithm we want to implement is the following.
    # givens: a foreground image, a background image, and a location to place the image
    # for each pixel in the foreground
    #   if it is not on the blue screen
    #      set the appropriate pixel in the background to the foreground color value
    

    From last week, we already know how to make a for loop over all the pixels in the image. But we may want to think a minute about how to state the if-statement for this task. A convenient test is to look at the ratio of the blue channel to the sum of all the color channels.

    # get the red, green, and blue color values
    # calculate their sum and store it in a variable as a float type
    # calculate the blue fraction as b / sum
    

    For the if-statement we want to implement the following test.

    # if the blue fraction is less than 0.38 or the sum is less than 50
    

    Inside the if-statement, we want to get the background pixel and set its color to be the same as the foreground pixel. The background pixel we want is at the (x, y) value of the foreground pixel plus the x and y offsets passed into the function.

    # grab the x and y values of the foreground pixel
    # calculate the position of the pixel in the backgrund unit
    # get the background pixel 
    # color the background pixel
    
  6. Create a main function that does the following.
    # Lets the user pick a file
    # Loads the file as the foreground image
    # Creates an empty background image
    # Calls copyIntoWithoutBlue, putting the foregound into the background
    # Shows the background image
    
  7. Now we're going to make a function, copyIntoEffect, that pastes a foreground image into a background image and implements the option of using a variety of color transformations on the foreground image as it is pasted. The foreground image should not be modified by this function.
    # Given foreground and background images, x and y positions, and a
    # string indicating an effect
    # 
    # For each pixel in the foreground image
    #    get the right background pixel
    #    if the effect string is 'normal'
    #       make the background pixel color the same as the foreground
    #    else if the effect string is 'negative'
    #       make the background pixel an inverted version of the foreground
    #    else if the effect string is 'swap'
    #       swap the blue and red channels on the 
    #    ...
    
  8. Test the above function by adding code to your main program that uses the function to put the foreground image into the background image using one or more of the effects.
  9. Now we're going to define a function that builds a simple collage out of two images. First, find a second image and save it to your Lab4 folder. It's best if you pick an image of your own, or ask your neighbor if you can use their blue screen image for now.
  10. Define a function collage2() that has four parameters that will hold the filenames of the two images you want to use plus the effect you want to execute on the images. The function will return the picture object it creates that holds the two images. The function should implement the following algorithm.
    # Open the two images
    # Calculate the sum of the width of the two images
    # Calculate the height of the tallest image
    # Create a new empty image that is sum x height
    # Use the copyIntoEffect function to place the two images
    # Return the collage image
    
  11. In your main program, use the setMediaFolder() to have the user select a folder in which to search for the pictures. Then just pass the filenames of the pictures to the collage2() function. Store the result of the collage function in a variable and then show the collage.

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