Homework 2

[Note: this webpage last modified Friday, 04-Feb-2011 19:44:51 EST]

This assignment will be handed out at the end of class on Wednesday, September 1 and is due before class starts (submitted electronically though) on Wenesday September 8.

For the problems dealing with pictures, I suggest trying them out on real life pictures - pictures of people, buildings, nature, etc. The website http://coweb.cc.gatech.edu/mediaComp-teach#Python for the textbook has a mediasources.zip file that you can download with sample pictures, if you want to use some of the same pictures that the textbook uses. You can also use your own pictures, pictures from the web, etc.

You should create a single Python file for your submission. It will include the code for the functions you are turning in, and you can include additional explanations as Python comments. For example, Problem 2, part b asks you to answer a question - you can put this as a Python comment below the swapRedBlue function you have created. See the description in problem 4 for how to do comments in Python code.

  1. (15 Points) The following code is supposed to be a function that takes a picture object that has been loaded into JES and compute the average value of red over all the pixels in the picture. But it has lots of mistakes. Correct the mistakes and include the corrected function in your Python file.

          def averageRed(pic):
            totalRed = 0.0
            for pixel in getPixels(pic)
              totalRed = totalRed + getRed(pixel)
            print "The total of red values for the picture is:", totalred
            avg = totalRed / getWidth()*getHeight()
          print "The average red value of the pixels in the picture is:" average
        

    After you have corrected all the mistakes, you can download the picture at http://www.kinnejeff.com/includes/madison_in_winter.jpg. If you save that picture and choose it when you call pickAFile, then your command area output should look like this.

          >>> pic = makePicture(pickAFile())
          >>> averageRed(pic)
          The total of red values for the picture is: 1.2764253E7
          The average red value of the pixels in the picture is: 123.32611594202899
          >>> 
        

    If you are unable to get rid of all of the mistakes, you can turn in an answer identifying which mistakes you were able to find.

    1. (10 Points) Write a function called swapRedBlue that takes as an input a picture that has been loaded into JES. swapRedBlue should swap the red and blue values on every pixel in the picture. So, after loading your swapRedBlue function into JES, I will be able to use it as follows.

      	    >>> pic = makePicture(pickAFile())
      	    >>> swapRedBlue(pic)
      	  
    2. (5 Points) What happens to a picture if you call swapRedBlue on the picture two times in a row? What happens if you call swapRedBlue on a picture three times in a row? How about four times?

  2. (15 Points) Write a function called myCoolEffect that takes as input a picture that has been loaded into JES. Your "cool effect" should be some change to the picture that was not given as an example in the textbook, in class, or elsewhere on this homework assignment. You can choose what effect to create. You should include the code for your function in the Python file you turn in. You should also include a line at the top of the code with a Python comment saying what your function is supposed to do. So your function will begin as follows.

          # This is a comment that Python ignores.
          # So you can say things to the person reading your code.
          # You should explain what your function is supposed to do.
          def myCoolEffect(pic):
            # bla bla, your code will go here.