Python 009: Image Processing

This will help you:

Work with new Python libraries, digitally process images and do this quickly on many files.

Anyone who's taken a photo with special effects on a phone camera or edited a photo for Instagram knows there's a ton of things that can make a picture look good after the shot. In this activity, you'll learn to process images with custom filters, transformations and effects. You'll create your own processes and see how to process a whole folder of images in seconds. We'll work with Pillow, a currently maintained version of PIL (Python Image Library) which has a ton of tools for analyzing and modifying images.

 [image] [image]

Time: 1-2 hours / Level: B3

You should already:

  • Be familiar with Python functions.

  • Install Pillow: try running pip install Pillow in the terminal.

Get the code and resources for this activity by clicking below. It will allow you to download the files from a Google Drive folder. Unzip the folder and save it in a sensible location.


  • contains functions for processing images using the Pillow library. Edit the process functions here.

  • and incomplete programs for processing images.

  • edits an image using my_transformation() from In the extras folder:

  • and complete programs for processing images.

  • applies the same processing function to all the images in a folder.

  • a utility program that translates colors.

  • rgb.txt: mappings used by


  • Pillow - an image library with several sub-libraries, or modules.

  • Image - a module with methods for working with Images.

  • Module - a library of functions and objects, or a subset of one.

  • Object - a collection of information, under one object name, which includes data about the object itself and functions or operations which it can do.

  • Method - a function that is specific to an object. Usually called by object.method()

  • Attribute - data that is specific to an object. Usually accessed by object.attribute

  • Image - ALSO an object, representing an image with methods to operate on the image.

  • Tuple - a group of pieces of data separated by commas, like (A, B, C) or 1, 2, 3.

  • Multiple assignment - assigning a tuple to a tuple, like R, G, B = a_tuple or A, B, C = 0, 12, 24

Step 1: Warm-up - Exploring Pillow

Open and look through it, in particular the functions pop_art() and quad(). Once you have looked it over, run the example function by typing python in the terminal. (You might need to move that file out of extras.) Do the following:

  1. Imports: Look at the imports at the top of the file, and find some of the functions that are being imported from libraries.

  2. Read about Pillow: Briefly scan this tutorial; some of the examples are good to see.

  3. Read about functions: Read the first paragraphs of descriptions for some Pillow functions used here. What do the arguments passed into them represent? What are they returning? Notice that most of the functions don't change the image passed in; they return a copy, which has to be assigned to a variable.

Image module functions: convert()resize()new()paste()save()

Notice that there is both an Image module, which owns functions like and, and an Image object, which is technically an Image.Image because it belongs to the Image module. In refers to the module doing the operation. In Image.convert()Image refers to an image object which is having convert() done to it.

ImageOps module functions: colorize()fit()

Most of the ImageOps functions take an Image object as the first argument, and do operations on it.

Step 2: Activity - Opening & Saving Images

Open and read through it. Complete each # TODO: line in the code so that it will create a modified image and save it. Check out an example here, or the documentation for open() and save(). Look at to see how ip.pop_art() and ip.save_image() are used.

To test the code, run python in the terminal. Once it works, make the same changes to and test it the same way. (Hint: it won't actually mirror the image unless you do the part below.)

Step 3: Activity - Using Processing Functions

Open the file Read the header comment for the function mirror(), then fill in the lines necessary to make the function work. The documentation for ImageOps.mirror(), and paste() will be useful to you. The documentation for attributes of Images will help you get the size. To test your code, type python in the terminal. If your function is working, it should display an image with its mirror.

Step 4: Make it your own

Open the file and look at the function my_transformation(). Currently it does nothing but take an image, assign it to a new variable and return it. Look at the functions in the Pillow documentation and create your own process for the image. Test your function by running python in the terminal.

Look at the functions quad() and pop_art() in Wherever there are color names for recoloring the images, you can put different color names in quotes to change the colors. mid is allowed to be None, and it will just fade from light to darkrgb.txt lists all the color names which are valid, but there's enough that you probably don't need to look.

Step 5: Going further

STOP AND DO THIS: To use, or, you should first move them out of the extras folder and into the folder above with most of the other files.

Open the file and read it over. See if you can understand what each part is doing, and look up any functions you don't understand. A lot of them are from the os.path module, which comes with Python but needs to be imported.

Run the program by typing python You will need to supply a relative directory path, which tells the program how to get from your current folder to the folder with images. If you're running it in a folder with images, just press enter. Otherwise, type the name of a child folder (like images). Read more about file paths here.

How can you change what function is used on each image? Try changing it and running it again.