Introduction to Computer Science
CompSci 101 : Spring 2014

Transforming Images

Images are a powerful way of conveying information and computers give us powerful way of modifying images. This lab involves manipulating pixmaps by transforming each color in the original pixmap using the same algorithm (such as darkening, inverting, posterizing, coverting it to grey scale). Your program will then perform several operations on these images to acheive a larger effect.

Images are stored by computers in a variety of formats, such as GIF, JPEG, TIFF, and PNG. These formats differ in how faithfully they represent the original picture, how well they can be compressed to reduce the space each image takes up, or how well they can be copied from one type of computer to another. However, no matter what format the image is stored in, it can always be represented of as a mapping of (x, y) pixel position to a color (the range of colors may be restricted to values of grey or just black and white). Thus, for the remainder of this project, we will refer to all formats of digital images as images.

This lab introduces you to the Python Imaging Library (PIL) which provides a simple interface for processing images. We will create basic color filters (RGB color model) that can be applied to images then combine them to create larger effects.

Can the search results be different file formats than your query (e.g., JPEG vs. GIF)? How much detail/quality can you leave out of the image without throwing off the results?


The most famous professional-grade image modification program is Photoshop, and images are often "photoshopped" to enhance certain properties. This photo was released by the Iranian government in 2008 to display the power of their new missiles, and they photoshopped it so that the missile launch appeared larger.

Image courtesy of Little Green Footballs.

Start by snarfing the lab code from the course website (alternatively, you can browse code here):

http://www.cs.duke.edu/courses/compsci101/spring14/snarf/

This code for this lab is divided into two Python modules, ImageUtils and Transforms.

Transforming Colors

You should implement the following transforms:

GreyScale. Create an image that has only shades of grey values by computing the average value of the pixel's three RGB channels and using that average value for all three channels of the new color. For example, if the pixel has the values (255, 0, 128) for its red, green, and blue channels, respectively; then the resulting color should have the values (127, 127, 127).
Invert. Create a photographic negative of the image by inverting each of the three RGB channels of the pixel. For example, if the pixel has the values (255, 0, 128) for its red, green, and blue channels, respectively; then the resulting color should have the values (0, 255, 127). In other words, each value is the other's opposite within the range of possible values from 0 .. 255. Thus, if this operation is performed on an image twice in succession, the image would appear unchanged.
Darken. Darken the image by reducing the values of each of the three RGB channels of the pixel by 25% of their original value. Note, this method should exactly undo the results of performing the Brighten filter (as long as none of the image's colors are already as bright as possible), so that doing one operation then the other should result in no net change in the image. Be careful not to produce a color value outside the range of possible values from 0 .. 255.
Brighten. Brighten the image by increasing the values of each of the three RGB channels of the pixel by 25% of their original value. Note, this method should exactly undo the results of performing the Darken filter (as long as none of the image's colors are already as dark as possible), so that doing one operation then the other should result in no net change in the image. Be careful not to produce a color value outside the range of possible values from 0 .. 255.
Solarize. Create a solarized version of the image by inverting the values of each of the three RGB channels only if they are too dark, i.e., less that 128. To do this, you should check the value of each channel and only if it is less than 128, invert its value (i.e, subtract it from 255). For example, if the pixel has the values (255, 0, 128) for its red, green, and blue channels, respectively; then the resulting color should have the values (255, 255, 128).