HW10: Image Processing I

25 points; due Fri 5/9 @ 9am.

Goals

The goal of this assignment is to learn about how images are stored and manipulated in a computer.

Setup and Requirements

Create a directory named image1 that will contain your code for this project. To facilitate reading in, storing, viewing, and manipulating image files in “standard” formats (like .jpg, .png, and .gif), we will use a module called imageManip. Download imageManip.py into your directory. Before starting this assignment, please read the documentation for imageManip so that you know the tools at your disposal.

You'll also need images to work with. I've put some examples in the images directory; you may download these and use them, or use you own. Note that animated GIFs don't work with our module.

The module is built on top of Pygame, a Python module oriented toward building games. Pygame is installed on the lab machines in the CMC, but can be something of a hassle to get installed on your own machine. You're welcome to try your luck at pygame.org, but the course staff provide no support whatsoever for getting Pygame on your own computer.

This is an individual assignment.

Your Task

Download the starter code, photolab1.py. Notice that if you run it, it works right away: if you provide an image filename as a command-line argument, it displays the image. Your task will be to add several functions to this program, and then change the main function:

oneColor(image, color)

Creates and returns a new image containing only the red, green, or blue aspects of the original image. color will be a string, either "r", "g", or "b", representing the user's choice. You can create the resulting image by setting the color value to zero in the two channels other than the requested ones.

Note that your function should not modify the input image. Make a copy of it (check the documentation for how you do that) and modify the copy.

invert(image)

Creates and returns a new image that is a conversion of the original image to “negative” form (like a film negative). The result should be an image where all the white pixels have turned black, all the black pixels white, all the pink pixels a sort of dark green-blue, and so on.

greyscale(image)

Creates and returns a new image where the value in each channel, in each output pixel, is the average of the values in the three channels in the corresponding input pixel.

saturate(image, k)

Creates and returns a new image where the pixel values have been given lower or higher saturation, according to the value k. If $k = 1$, then the output image is the same as the input. If $k = 0$, the output is in greyscale. Values in between fade from full color to grey. If $k > 1$, then the colors are enhanced.

The starter code provides you with a helper function saturatedRgb(colors, k), which, given an RGB triplet, will return a modified triplet. So you don't actually have to implement the contrast calculations yourself (though if you're curious, you should totally investigate the source code!); this is just an exercise in calling a function to do a pixel transformation.

main()

Change the main function so that it tests the above functions by using an image file and making one-color, inverted, grayscale, and saturated versions of it appear on the screen. Don't forget to use a raw_input() statement at the end of your program so that you can view the images before your program exits!

When we test your code, we will remove your main() and add our own. If you have carefully followed the above specifications, your code should work with our version of main(). As an example, we should be able to write code like this and have it work:

window = imageManip.DisplayWindow(1024, 768)
img_orig = ImageManip('dave.jpg')
red_only = oneColor(img_orig, 'r')
red_only.draw(window)

Please don't call your main() function at the bottom of the source code, as we have been doing in previous assignments. Instead, notice that the starter code ends with an if-statement that runs main() under a certain condition. This condition is true when your program is invoked directly from python; that is, if you run:

python photolab1.py dave.jpg

your main function will get called, and sys.argv[1] will be "dave.jpg". So there's no need to change this aspect of the code at all.

Grading and Submission

Remember to put your name at the top of your source code!

Submit your program, photolab1.py, via Moodle.

Good luck, and have fun! Remember that lab assistants are available in the evenings in the CMC to help out if you need it.

This assignment originally designed by Dave Musicant, with modifications by Sherri Goings and Andy Exley (and then me, of course). It takes a village...