Jason M. Grant

Assistant Professor of Computer Science

Image Manipulation, Pt. 1

This week, we will implement some image transformation functions. If you don’t yet have a resized image of your own to work with, for now you can use either sample image 1or sample image 2.

Put the image in the same directory as your Python file.

Create your new Python file and then put this line at the top:

from PIL import Image

This imports the library we will be using to manipulate the images. The library is called Pillow (Pillow is an updated version of an older project called PIL, thus the name). Here is the documentation.

This is a different import format than we have used previously. This imports a single item out of a library (in this case Image). The following command opens an image from the same directory (passed in as a string) and returns an image object. The PIL module can open a variety of different image formats. Generally, the images you have available will be .jpg, .png, or .gif files, all of which can be opened.

Run this file, despite the fact that it doesn’t appear to do anything. It will import the Pillow library and set the working directory so we can find your image.

In the Python console, type the following line (updating the string containing the file name as appropriate).

image = Image.open('statue.jpg')

This will open the file and store a representation of the contents in a variable called image(you can, of course, use any variable name you like).

To see the image, you can call its show()function.


When you are ready to save the image back out to the file system, you can call the save()function like this:


PIL will figure out what format to save the file based on the suffix you supply the name. The file will turn up in the same working directory as your input images and source files. Save as ‘.png’ rather than ‘.jpg’, because PIL’s default jpg processing looks horrible.

Our style of working with media will be that we open images on the console, pass them into functions that return new images, and then show or save the results.

Example: Isolate the blue channel

As we discussed in class, every pixel in a color image is made up of red, green, and blue data. For our first function we are going to isolate the blue channel by setting the values associated with the other channels to 0.

Here is the function.

def justBlue(image):
    """Return a new image containing just the blue values from an image.

    The input should be a PIL image object created using something like
    image = Image.open(filename), where filename is a string.
    # create a copy of the original image so we don't lose the original
    newImage = image.copy()

    # create our 2D structure of pixels from the image so we can work with individual pixels
    pixels = newImage.load()

    # get the bounding box -- this is returned as a tuple and we use some python
    # magic to automatically break the tuple up into four variables
    minX,minY,width,height = image.getbbox()

    # iterate over every pixel in the image
    for y in range(height):
        for x in range(width):
            # get the rgb value of the pixel (as a tuple in the order (red, green, blue))
            rgb = pixels[x,y]

            # set the pixel to a new RGB value (in this instance, we keep the original
            # blue value and set the red and green to 0)
            pixels[x,y] = (0,0, rgb[2])

    # return the new image
    return newImage

Run this on your image and use show()to look at the result.

>>> blueImage = justBlue(image)
>>> blueImage.show()