PreLab 04

Pictures and Images
Due by the beginning of lecture on Wednesday, September 21st

In this prelab you will formulate some of the ideas necessary to complete Lab 04. Please type up your solutions, and hand in a paper copy before the beginning of lecture on Wednesday. Remember, no late prelabs allowed!


Read about one of the first Computer Scientists, Ava Lovelace, who predated computers by a century.

1. Did you do the reading? (Yes or no will suffice.)

Method Practice

2. What is the output of the following program?
      def main() : 
      def blarg(x) :
      def blorf(y) :


3. What is the output of the following program?
      def main() :
         x = 2, y = 7
         print(x, y, end="")
         x = blunk(x, y)
         print(x, y, end="")
      def blunk(y, x) :
         print(x, y, end="")
         y = y * y
         x = x//2
         print(x, y, end="")
         return x*y


Using Exceptions

4. (From the book.) Why is using try/except to deal with errors often preferable to using other conditional decision structures (variations on if)?

5. Write a chunk of code with the following behavior. First, it should prompt the user to enter four numbers (x1, y1, x2, y2) separated by commas. It should then compute and print the slope of the line that joins the points (x1,y1) and (x2,y2). Your code should handle at least three possible exceptions.

Picture This

In the lab proper, you'll be using graphical primitives (rectangles, circles, lines, etc.) to create an image of your own choosing.

6. For now, sketch a picture that you'll use as a blueprint for your eventual picture program. Some past projects: a hippo birthday party; a cityscape; a unicorn fighting a robot; happy flowers. Your sketch should be on it's own sheet of unlined paper, and should indicate color as well as form. You can use markers, pens, pencils, watercolors, etc. You aren't expected to be Michelangelo (or any other TMNT for that matter), but you shouldn't just make a smiley face and call it a day. Unless it's an undeniably awesome smiley face.

Image Manipulation

In this portion of the lab you'll create a nifty program that reads in an image and does a sequence of modifications to that image, as specified by the user. Things like negating the image, blurring it, changing the contrast, etc.

Describe the Problem:

Your program should prompt the user to enter the filename of an image to edit. It should then repeatedly prompt them for filters to apply, and show the cumulative effect of these filters on the specified image.

Understand the Problem:


Consider the image of crayons above. There are numerous transformations we might apply to this image. For the lab, you'll be creating a program capable of the following operations:

  1. Make Negative
  2. Make Grayscale
  3. Increase Contrast
  4. Posterize
  5. Flip Horizontally
  6. Scroll Horizontally
  7. Zoom
  8. Blur
  9. Your Own Effect (I)
  10. Your Own Effect (II)

Examples of these operations are given below.

Original Image:
Negative: Greyscale:
flip mirror
Posterize: Contrast:
scroll negative
Flip: Scroll:
scroll negative
Zoom: Blur:
zoom poster
Other Ideas
Find Edges: Tiled:
find edges tiling
Shear: Bizarre:
shear bizarre

Design an Algorithm:

Your program should begin by printing a welcome to the user, informing them just how fortunate they are to have stumbled upon your very own image editor. You should then prompt the user in the console to pick a file to load in. As will be standard from here on out, you'll want to be robust against bad user input, so if the user enters something that isn't a valid file and an exception is thrown, you can catch it and prompt them again for a file.

Once you have an image loaded, display it. Then use a while loop to repeatedly print a table of possible operations, prompt the user to select one of these operations to apply to their image, apply the selected operation (create a distinct function for each operation), and display the resulting image. Thus, the user might choose to reflect the image, then increase the contrast (of the now reflected image), and then blur (the now reflected and contrasty image). At each step, the user should be able to see the resulting image. Again, be sure to handle bad input from the user.

Of course, this still leaves out the details of each operation, for which you will want to write pseudocode before you start writing functions. Details for some of these operations are given below.

Negative: The negative of an image is creating by inverting each color channel. So if the red value of a pixel were 255, it should become 0. If it were 254, it should become 1, and so on, down to 0, which should become 255. Similarly for green and blue.

Grayscale: Shades of gray have the same red, green and blue value. To convert an image to grayscale, you want to set the red, green and blue values all to the average value of the three channels of the original pixel.

Posterize: A typical pixel can have one of 256 value for each color channel. In a posterized image, this number is drastically decreased. Each color channel value should be rounded to the nearest multiple of 32.

Increase Contrast: When increasing the contrast, color values at 128 should be unchanged. For any other value x, the difference between x and 128 should be scaled a factor of 2. For example, a color value of 129 (1 above 128) would become 130 (2 above 128). 125 (3 below 128) would become 122 (6 below 128). Just remember that you'll need to stay between 0 and 255.

Scrolling: Scrolling should ask the user to specify some number of pixels, and should then shift the image that many to the right. Pixels that would fall off the edge of the image should wrap around to the other size. Modular arithmetic may come in handy here.

Zoom: This method result in an image of the same size as the original, but consist of the center of the image blown up by a factor of 2. So if the image has width w and height h, zooming should expand the middle w/2 by h/2 region to fill the whole picture.

Blur: When you blur an image, you set the color of each pixel to be the average of the 9 pixels in the 3 x 3 square centered at that pixel (i.e. the average of the original pixel and its original 8 neighbors). You'll probably want to create a new Picture object; otherwise, you'll be adjusting pixel values that you'll need for subsequent calculations. Be careful at the borders, not all pixels have 8 neighbors!

In the lab, you'll be writing a program that performs many of the above transformations on images. In preparation, you'll be generating pseudocode to perform some of these operations. As an example, we consider the task of removing all red from an image, and give a pseudocode solution is given below. Give your own pseudocode solutions to the remaining problems.


For some of these operations, you may want to create a new image rather than by simply modifying the existing image. You may assume you can generate a copy of an image.

You can not change the position of a pixel. You can, however, read the r, g and b values for any pixel, and you can set the r, g and b values of any pixel.

Ex. Remove all red from an image.
    Sample answer:
    loop over x values from 0 to W - 1 inclusive (You can assume W is the width)
        loop over y values from 0 to H - 1 inclusive (You can assume H is the height)
            change the red value of the pixel at (x,y) to 0
7. Negate the image.

8. Convert the image to grayscale.

9. Flip the image.

10. Scroll the image to the right by d pixels.

Honor Code

If you followed the Honor Code in this assignment, write the following sentence attesting to the fact at the top of your homework.

I affirm that I have adhered to the Honor Code in this assignment.