code directory and handin
Image courtesy of Little Green Footballs.
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.
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.
Image.open(image_filename)
, opens an image
file. To use the image in a program
assign it to a variable (e.g. im =
Image.open(image_filename)
).
im.getdata()
returns a list of the pixels
from the image, where each pixel is a tuple of 4
integers, representing the red, green, blue, and
transparent components of the pixel. Each of these
values is between 0 and 255, inclusive. The following
example:
list_pixels = im.getdata() for pixel in list_pixels: print pixelcould show something like this, depending on the RGB values in the image.
[(255, 0, 0, 255), (0, 255, 0, 255), (0, 0, 255, 255), ....]During this lab, we won't care about the transparency of the pixels; we're interested in the color components, that's the (r,g,b) part of the (r,g,b,a) tuples stored in the list.
im.putdata(list_pixels)
takes a list of
pixels as an argument (it must have length equal to the
original image's size) and stores these pixels into
im
's internal representation of the image.
im.save(image_filename)
takes a string as an argument and
saves the image back to the filesystem. This is analogous to opening a
text file in "write" mode, writing to it, and closing it.
im.show()
displays the image using the
default image viewer on your computer.
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.
Denoise.py
, modify the code such that the
red component of every pixel is removed.
remove_blue
and remove_green
that remove the blue and respectively the green component.
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:
apply_filter
show_image_transformed
save_image_transformed
Please take a look at ImageProcessing.py
.
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.
show_image_transformed
and
save_image_transformed
both of which call
apply_filter
as a helper function.
showTransformedImage
to view the results.