Part A: Warmupgram

In the main part of the lab this week, you’ll be writing a program that uses a while loop to ask the user which filter they would like to apply to a picture (like on, e.g., Instagram). To start the warmup, you will implement two basic filters, which will get you used to the important functions in the picture module. These filters will be in a separate file for this warmup, but you can integrate them into the main lab later.

Start by reading through the starter code for warmupgram.py. Notice the main function at the end of the file which loads the crayons test image, applies the two filters you will implement as part of the Warmup, and then shows the picture.


The first filter you’ll implement is grayscale. Below, you’ll see an image of crayons, as well as the grayscale version.

The first thing you’ll need to do is get an image loaded and displayed. Check out warmupgram.py for some starter code. The main thing you need to add is code in the middle of the file. An explanation of what’s needed is included as a comment.

Some functions you’ll want to know about:

picture.get_green(image,x,y) returns the green value of the pixel in image at location (x,y). It will be returned as a number between 0 and 255. picture.get_blue(image,x,y) and picture.get_red(image,x,y) do similar things for the colors blue and red.

picture.set_green(image,x,y,val) changes the green value of the pixel in image at location (x,y) to val. val must be an integer between 0 and 255. picture.set_blue(image,x,y,val) and picture.set_red(image,x,y,val) do similar things for the colors blue and red.

You can also use red,green,blue = picture.get_pixel(image,x,y) and picture.set_pixel(image,x,y,(rval,gval,bval)) to get/set all three values simultaneously. The RGB values returned by picture.get_pixel() are stored in three variables: red, green, and blue, which you should feel free to rename.

Vertical Flip

For this filter, you will complete vertflip in warmupgram.py that turns an image upside-down.

You’ll have to be careful for this one: if you just try to use picture.get_ and picture.set_ functions, you’ll end up overwriting the values of pixels you’ll need later. To solve this problem, you’ll first have to write a copy() function to duplicate your image. It will take as input an image object, like the one loaded by the starter code, and will create a new image object, called imagecopy. To write your copy() function, you’ll need to use picture.get_ and picture.set_ functions to copy all the pixel data from image to imagecopy before returning imagecopy.

Once you’ve got copy() written, use it to implement a vertical flip filter, which turns the image upside down. You’ll need to make a copy of the image to use as a reference as you go through and set the pixels in image to be upside-down. Keep the copy untouched as a reference, and design your program so that image contains the flipped picture. In the lab, you’ll do a lot more passing and returning of image objects like in warmupgram.py. The key in the remainder of the lab is to know when you should modify the original image versus when you should use a copy.

Part B: Debugging Practice

For this week’s debugging practice, you’ll consider some function-related bugs that you’re likely to encounter at some point as programmers. Solve each problem by first thinking on your own for a few minutes, then discussing what you figured out with your partner.

You’re going to be debugging three calculator programs today. They were written by someone who didn’t seem to realize that you could just do basic calculations in the console. Instead, each of the three programs runs a while loop, which asks the user whether they want to multiply, exponentiate, or quit. Multiplying should ask for two numbers, say, x and y, and print their product. Exponentiating should ask for two numbers, a base and an exponent, and raise the base to the power of the exponent. Unfortunately, none of the three programs, calc1.py, calc2.py, or calc3.py does both tasks successfully.

For each program, decide on some test cases, and run the program on those test cases. You should make sure to try both multiplication and exponentiation (though both aren’t necessarily broken), and make sure that your test results are different when the inputs are switched. See if you can figure out what the bugs are in each program, and how to fix them. Better yet, try to understand what the person who wrote the code might have been confused about.


The while loop for each program is correctly implemented; you should be looking for bugs with the functions and their usage.