Compsci 6/101, Spring 2011, Lab 8

code directory and handin


Images are a powerful way of conveying information, and computers give us powerful way of modifying images. 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.

Image Processing with Python

This week's lab introduces you to the Python Imaging Library (PIL). We will have fun creating basic color filters (RGB color model)that can be applied to images. PIL provides a very simple interface for processing images.

Start by snarfing the Lab 8 code from the class website. Alternatively, you can browse code here .

In this lab we will be using a few functions from the Image library. Some are shown in the code snippet below. This code opens an image, displays it, gets all pixel/RGB values from the image, sets all the red values to zero, and then displays the image again. We'll go over this in more detail in lab and in this writeup.

im = Image.open("vases.png") im.show() data = im.getdata() ndata = [(0,b,g,a) for (r,g,b,a) in data] im.putdata(ndata) im.show()

The images displayed are shown below, unaltered on the left, no red on the right.

Here's a brief summary of the commands we're using for Image types.

Although PIL can do many other things, these are the parts we will use today. We will be working with an image of colored vases. If you accidentally overwrite the original with a transformation, you can re-download the image here. It depicts custom made vases from Tsuga Studios

Denoise (10 minutes)

Image noise.png (see below)

This image has been shoot with a broken camera. The results of the technical inspection concluded that the camera light sensor gave random values for the blue and green component, while it was diminishing the value of red component by a factor of roughly 10 times. We would like to "denoise" the picture in such a way it is interpretable by humans.

  1. Since the blue and green components have random values, a first step in our denoise process will be to remove the blue component in every pixel of the image. Don't worry we still have the red component to reconstruct our image. Open file Denoise.py, modify the code such that the red component of every pixel is removed.
  2. Further modify the code such that the green component is also removed from every pixel.
  3. Write two functions remove_blue and remove_green that remove the blue and respectively the green component.
  4. Modify the red component of every pixel by multiplying with 10, in order to get the reconstructed image.

Image Filters

We are going to develop a framework for image filters in Python. Our framework will implement the following filters: remove color components (red, green or blue), invert the image, transform the image in a grayscale version, increase or decrease the brightness and solarize. Given that we have so many filters, we would like:

Please take a look at ImageProcessing.py.

  1. Write apply_filter. Its first argument is an image filename, its second argument is a function which transforms a pixel (it takes a pixel, which is a tuple of 4 integers, as an argument and returns a pixel) and the third argument is the filename of the newly produced image. Please use the remove_blue or remove_green to test your functions.
  2. There are two main things we want to do with images: view them and save them. Write show_image_transformed and save_image_transformed both of which call apply_filter as a helper function.
  3. Our framework is complete! Now we can have some fun by actually modifying pixels using other transformation functions. Fill each one in, and then use showTransformedImage to view the results.
  4. If there is time left over, look at TinEye. This is a search engine whose queries are images, and whose search results are places that this image can be found. Experiment with it; try to figure out how it works. 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?