CS 107: Pictures and Sounds: Programming with Multimedia

Kalamazoo College

Spring 2008

Mini-Lab: For Loops
for Manipulating Pixels in a Picture

 


Introduction

In this mini-lab you will gain practice with using Python for loops to process all pixels in a picture.



Modifying pictures pixel-by-pixel

  1. Start up JES. Add comments to the top that describe the file as a whole, such as:
    # Mini-Lab: Functions that Manipulate Pixels in a Picture
    # Your Name
    # Today's Date
  2. Type in and run Recipe 8 on p. 56 of the textbook. Remember to add a comment for this particular recipe, such as:
    # Recipe 8 from p. 56: Reduce the amount of red in a picture by 50%

    When running this function, you can use a "hard-coded" file name, as in the example at the bottom of p. 56, or you can use the pickAFile function from previous labs; for example:

    picture = makePicture(pickAFile())
    Debugging Tip: If the function does not work correctly, check that you remembered the colon at the end of the first line of the function definition and that you spelled all of your variable names and function names correctly.  After any edits, remember to click on the Load button before calling your function.
  3. Read Section 3.3.4 on p. 61 and then type in and run Recipe 9.  Remember to add a comment for each recipe. If the picture(s) you are using have too much red in them, you will get an error because the new red value will be greater than 255. You may want to make an empty gray picture and try increasing the red in it.
    Analysis Questions: What similarities do you see between the decreaseRed function of Recipe 8 and the increaseRed function of Recipe 9? What differences are there between the two functions?
  4. Create and run a function called showRedChannel that sets the blue and green values for each pixel in an image to be equal to the red value. For example, a pixel with the value (RED=123, GREEN=18, BLUE=222) would be changed to (RED=123, GREEN=123, BLUE=123).
    Analysis Questions: How does showRedChannel affect the appearance of your picture? What happens to regions of the original picture that were red? What happens to regions of the picture that were blue?
  5. If you have time, read the beginning of Section 3.4 on pp. 61 - 62 and then type in and run Recipe 11.  Remember to add a comment for this recipe.

Print your results

  1. Print the functions in this file and hand them in.

If you have time:

  1. Type in and run Recipes 13 and 14 on p. 67 of the textbook.  These will become the beginning of Lab 2.