CS 107: Pictures and Sounds: Programming with Multimedia

Kalamazoo College

Spring 2008

Lab 2: Simple Picture Manipulation

 


Introduction

In this lab you will become more familiar with using Python for loops to manipulate colors in a picture.



Generalizing increaseRed and decreaseRed

  1. The increaseRed and decreaseRed functions are so similar, that we might ask why there are two different functions at all.  Create a new function (for example, by copying and pasting either one of them) that takes an additional parameter representing the multiplier to use when modifying the amount of red in a picture.
    Design Questions: What would be a clear, meaningful name for your new function?  What would be a clear, meaningful name for the new parameter?
    Use the parameter rather than a constant multiplier value in the body of the function.
     
  2. Load and run your new function to test it.  Make sure that you can use it to achieve the same result you achieved previously with the increaseRed and decreaseRed functions.
     
  3. If someone wanted to use your new function, do you think it would be obvious to them what the second parameter does and what type of value they should pass in as that parameter when they call the function?  Make sure that the comments you provide for this function make clear the purpose of the function, the purpose of each parameter, and maybe even an example of how to call it.
  4. Create similar generalized functions for green and blue, with appropriate function comments. Test them as you did your previous function.

Creating variations on a picture

  1. Modify your makeSunset function from the previous mini-lab to be similar to that in Recipe 12 (p. 63), but using your new, generalized functions rather than the reduceBlue and reduceGreen functions. Test your modified function by loading and running it.
  2. Now test the makeSunset function with a number of different pictures. Choose one that you particularly like and save it using the JES writePictureTo function at the bottom of p. 49. For example, suppose my picture was captured in the variable myPict. To save this picture as a JPEG image, I would type
    writePictureTo(myPict, pickAFile())
    When the file selector window opens, I would navigate to the folder where I want to save the image. I would then type in a name for the image, such as sunsetPic.jpg. Be sure to include the .jpg file extension so that it gets saved as an image file.

    (The writePictureTo function is described in the section of Chapter 3 that we have skipped, but you don't need to read anything else in the section to understand how the writePictureTo function works.)

  3. Type in (providing appropriate comments) and run Recipes 15 (p. 68), 16 (p. 69) and 17 (p. 70).  For each function, create and save a modified picture.
  4. Now it's time to be creative! Write a new function to create an effect of your own choosing. For example, you might try to modify a picture to give the effect of a red sky at night, an approaching tornado, or a landscape on another planet, or you might try switching the red, green, and blue color values around, or setting one of the color values to 0, or anything else you like. Save a picture you created using your new function.
  5. (Just for fun. Don't spend time on this exercise until the other parts of the lab are finished!) Write a new function that creates a full moon effect.
    Design Questions:
    • How do you think you might achieve a full moon effect? Jot down some notes to yourself of what you plan to try, before you try to fully implement it in Python.
    • What would be a clear, meaningful name for your new function?
    Load and run your new function. Did you get the effect you were striving for? If not, experiment with your function, making small changes, loading your function, and running it. When you have the effect you want, create a picture to save.

Print your results

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

Post your results

  1. During this lab, you probably created some neat results. Create a new Lab 2 web page to display at least two of these. You should include the original picture, as well as the modified picture, and a short description of what modifications you made to the original picture.
  2. Copy your web page and all the images that should be on it to your personal web space on kzoo.edu. (You may want to review the instructions in Lab 1 if you've forgotten how to do this.)
  3. Include a link to your new Lab 2 web page from your course page. Bring up your course page in a web browser and test your link.