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.
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.
Index
image_processing.py
: contains functions for processing images using the Pillow library. Edit the process functions here.mirror.py
andpop_art.py
: incomplete programs for processing images.my_transformation.py
: edits an image usingmy_transformation()
fromimage_processing.py
. In theextras
folder:quad_collage.py
andrecursive_image.py
: complete programs for processing images.fast_file_process.py
: applies the same processing function to all the images in a folder.color_converter.py
: a utility program that translates colors.rgb.txt
: mappings used bycolor_converter.py
.
Glossary
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
orA, B, C = 0, 12, 24
Step 1: Warm-up - Exploring Pillow
Open image_processing.py
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 quad_collage.py
in the terminal. (You might need to move that file out of extras
.) Do the following:
Imports: Look at the imports at the top of the file, and find some of the functions that are being imported from libraries.
Read about Pillow: Briefly scan this tutorial; some of the examples are good to see.
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.