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 reduceRed

  1. The increaseRed and reduceRed 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 reduceRed 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 so that instead of looping through all the pixels and changing their green and blue values, it just calls your new, generalized functions. These new functions will take care of the looping and color changing. Test your modified function by loading and running it.
    Debugging Tips: Remember that your new functions will return a new picture with the colors modified. So, after you change the green values in the picture, you need to make sure you pass the result you got from that function into the function to change the blue values. You will then return the picture that gets returned from the function that changes the blue values.
    Analysis Questions: Is it necessary to create a new picture that is a duplicate of the original picture in the beginning of the makeSunset function anymore? Why or why not? If it is not necessary, remove that code from your function.
  2. Now test the makeSunset function with a number of different pictures. Choose one that you particularly like. We are going to save it using the JES writePictureTo function. For example, suppose my picture was captured in the variable myPict. To save this picture as a JPEG image on a PC, 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.

    To save this picture as a JPEG on a MAC, I would type

    writePictureTo(myPict, pickAFolder()+"sunsetPic.jpg")
    where sunsetPic.jpg is whatever you want to name the image file. When the folder selector window opens, I would navigate to the folder where I want to save the image.
  3. Type in (providing appropriate comments) and run the negative, grayscale and weightedGrayscale functions from the Pictures and Loops notes. For each function, create and save a modified picture.
  4. Now it's time to be creative! Experiment with other variations of increasing or decreasing color values, or setting them all to some value. For example, you might see what happens if you increase or decrease red, green, and blue by the same amount, or if you increase blue and decrease red and green, or if you switch the red, green, and blue values. Once you find some combination that you find is appealing, write a new function to create this effect. Give this function a name that represents the effect. For example, if you create a red sky at night effect, name the function something like redSkyEffect. Add a comment at the top of the function to describe what it does. Save a picture you created using your new function.

Submit your results

  1. Submit the file containing your functions from this lab via .

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 people.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.