NO LECTURE WDS. NOV 26!
Here are tips from your classmates
on installing PIL.
We will do some image processing in this project, using the Python Imaging Library, PIL. You may use any input image you like; please make sure it is a .jpg file, and not too large (less than 50Kb; right-click on the image and click on 'Properties' to check the size).
If you are working on your own machine, you should download and install PIL from the PIL download page (Mac users, get PIL from these great pages of Python 2.5 packages or Python 2.4 packages). Make sure you download the version that matches your version of Python (I have Python 2.5, not 2.6). Try running the testPIL.py program to make sure you can display an image (try it with my turkey.jpg, then your own image file). If you have a problem and solve it, please let me know and I will add your suggestion to this page of tips provided by members of the class. If all else fails, you can work in one of the labs.
We will do two effects as part of this program; each effect must be implemented using functions, as described below. The first effect is to turn a color image into black and white. The second is to "pixelate'' the image: replace squares in the image with the color of one of the input pixels from the middle of the square. You should use 10x10 squares, or you can let the user enter a number k and use kxk squares. You are of course welcome to add other effects as well; we will do several in class, and you can think up your own.
Your program should start by asking the user for the file containing the image. Then it should ask which of the two (or more) effects they would prefer. For instance:
Enter the name of the image file:
Displaying black and white image.
Opening turkey.jpg.
This program does the following effects:
Black and White (B)
Pixelate (P)
Enter your choice: B
If you work with a partner, you should get together, sit down, and work at the same computer. That way you'll both learn the things you'll need to know to pass the tests.
Step 2: Write a function that takes a color tuple as input, and produces a color tuple as output which represents an appropriate shade of gray. Recall that a tuple represents a shade of gray when the red, green and blue components are all the same. Compute the gray value for a colored input by averaging the red, green and blue components of the color. For instance, on the input tuple (99,0,0) your function should output the tuple (33, 33, 33). Test your function by calling it on a few different example color tuples, and then printing out the results.
Step 3: In your main program, write a loop which calls your new function on every pixel in the image, and changes the color of the image pixel to the shade of gray computed by your function. You can use the getpixel and putpixel methods to read and write a color.
Step 4: Comment out the loop in the main program. Now, write a function that takes color and a location as input, and colors the 10x10 square in the image, with the location as its upper-left corner, with the color. Again, you should use the putpixel method. Test your function by having your main program use it to color a square in the middle of the image bright red.
Step 5: In your main program, write a for loop which colors every 10x10 block in the image with the color of one of the pixels from the center of the block, using the function you wrote in step 3. Display the pixelated image. Try it on my input file, turkey.jpg, and see if it looks like the image above.
Step 6: Add the part at the beginning of the main program that lets the user pick one of the effects. Uncomment your black-and-white function, so that the user can use it.
Hand in just the program on myUCDavis. Hand in your input image as well as your program.