111 lab8

Computer Science 111 – Fundamentals of Programming I

Programming Project 8

Due date: 11:59 PM Friday 13 November 14

In this project, you will solve some problems in image processing.

Note: In your turnin, please name the folder (project8) and file (testimage.py) exactly as specified. You will lose points from now on if you deviate from this convention by using capital letters, blank spaces, etc.

WarmupDownload and unzip the project8 directory with the files smokey.gif, ken.gif,testimage.py and images.py to your account. Launch IDLE by running idle3. Opentestimage.py in IDLE and run the program.

A window will pop up with an image. When you close the image window, the program changes the image to black and white and pops the window back up to display the changes. To quit this program, close the image window again.

To compare images before and after processing, side by side, you can run the same program in two different terminal windows.

Note that the testimage.py file contains two functions to process images and two other functions to run tests. Once the script has been loaded from IDLE, you can run any of the test functions from the IDLE shell prompt. Note also that you can supply optional arguments for the file and other data to these functions. You should now run each test function with both image files from the shell prompt.

To Turn in

You will define four functions in these exercises. All of the definitions should be in thetestimage.py file. In addition, you should define a test function to test each image processing function. The test function loads an image, displays it in a window, transforms it, and displays the result so you can compare before and after. The functionsblackAndWhite and testBlackAndWhite collaborate in this manner in the script that you already have. Thus, your file should contain four new image-processing functions, four new tester functions, and a call of the function currently being tested.

1. Define and test a function named posterize. This function expects a triple of RGB values as a second argument and converts the colors of the image to that color and white. When the function is called in its tester function, pass a random color as its second argument. The default value of this argument should be black. (Hint: copy / paste / rename / modify the code and test for black-and-white conversion.)

  1. Define and test a function named grayscale. This function uses the psychologically accurate luminance factors discussed in lecture to transform the color values in the image.
  2. Old-fashioned photographs from the 19th century are not quite black and white and not quite color, but seem to have shades of gray, brown, and blue. This effect is called sepia. Define and test a function named sepia that converts a color image to sepia. This function first calls the grayscale function to convert the color image to grayscale. Here is a code segment for then transforming the color component value of a single pixel to achieve a sepia effect. Note that the value for green does not change.
       if red < 63:
           red = int(red * 1.1)
           blue = int(blue * 0.9)
       elif red < 192:
           red = int(red * 1.15)
           blue = int(blue * 0.85)
           red = min(int(red * 1.08), 255)
           blue = int(blue * 0.93)
  3. The edge detection function returns a black and white image. Think of a similar way to transform the color values so that the new image is still in color but is merelysharpened. Then define a function named sharpen that performs this operation. The function expects an image and two integers as arguments. One integer represents the degree to which the image should be sharpened. The other integer represents the threshold used to detect edges. (Hint: A pixel can be darkened by making its RGB values smaller. Use max to keep the lowered value from dropping below zero.)

5. Extra credit (10 points). Define a Python function named rotateRight. This function expects an image as an argument and returns a new image. The new image represents the original one rotated 90 degrees clockwise.

Turn in a copy of your project8 directory.

Thought Question (no turnin required): Which of the functions – the ones already inimages.py as well as the ones you created – use the clone method, and which do not. Why?