Lecture 24


MCS 275 Spring 2022
David Dumas

Lecture 24: Pillow

Course bulletins:

  • Worksheet 9 posted.
  • Read the Project 3 description.
  • Project 3 due 6pm on Friday March 18.
  • We'll discuss Project 3 a bit today.

Install Pillow

It is a package you can install with pip.

python3 -m pip install pillow

Or check the official install instructions.

Pillow and PIL

Pillow provides a module called PIL.

The name difference is for historical reasons.

Pillow replaces a module named PIL that was created for Python 2, and which hasn't been updated since 2011.


There are two basic types of image files you will encounter: vector images and bitmap images.

Vector images

Store instructions about what to draw (a circle here, a line there, etc.); can be viewed at any size without loss of sharpness.

  • e.g. PDF, SVG, PS, EPS, DXF
  • Also TTF, OTF, WOFF fonts
  • Good for drawings, diagrams, text

Displaying a vector image is a complicated operation! (PS, PDF are full programming languages.)

Bitmap images

A rectangular grid of colors, meant to be displayed with each color corresponding to one pixel on the display device; becomes blurry or blocky if you zoom in.

  • e.g. PNG, JPEG, GIF, BMP, TIFF
  • Good for photos, screenshots

JPEG (aside)

JPEG is for photos. That's what the P stands for.

JPEG compresses the image data, discarding some of it. Images with sharp edges will look bad as a result.

Use PNG for anything other than photos, unless it is essential to have a small file size.

JPEG is an amazing application of the Fourier transform, and I think everyone should learn a bit about it. I used all the self-control I could muster to say no more about it here.

Pillow is for bitmaps

Pillow is for working with bitmap images. It can read and write PNG, JPEG, GIF, BMP, TIFF, and more.

It is useful for format conversion, low-level image operations (e.g. make this pixel red), and provides some high-level operations too (e.g. blur, sharpen, convert to grayscale, ...).

Load, Save, Create

PIL.Image is a class that represents bitmap images.

        from PIL import Image

        img = Image.open("adorable_kitten.png") # load
        img.save("discord_avatar.jpg") # save

        # new color image, 1920x1080 resolution, all white
        img = Image.new("RGB",(1920,1080),color=(255,255,255)) 


An image file can store various amounts and types of color data. Pillow encodes this in a mode string:

  • "1" - 1 bit per pixel, 0=black, 1=white
  • "L" - 8 bits per pixel, 256 shades of gray. 0=black, 255=white. Also called "grayscale".
  • "RGB" - 24 bits per pixel, 8 each for red, green, blue. Also called "true color". Most common.

These are common modes, but there are lots more.

Pixel coordinates

A location in a bitmap image is specified by a pair of integers (x,y). The upper left corner is (0,0). Coordinate x increases as you move right, and y increases as you move down.

Note the y direction is opposite from mathematics.

Working with pixels

Suppose img is a PIL.Image object.

Set a pixel color (draw a tiny dot):

        # make pixel at (10,20) magenta
        img.putpixel( (10,20), (255,0,255) )

Get a pixel color:

        # returns color of pixel at (10,20)
        img.getpixel( (10,20) )


Some other methods of PIL.Image:

  • convert - Conversion to a different mode, e.g. from true color to grayscale.
  • crop - Crop (remove all but a smaller rectangle).
  • resize - Stretch or compress to a new size.
  • paste - Draw another image on this one.
  • transpose - Do any combination of mirroring and rotating by multiples of 90 degrees.

Lots more in the documentation.

Animated GIF

If you have PIL image objects frame0, frame1, frame2, etc.


duration is milliseconds per frame

loop=0 means loop forever (loop=5 means play 5 times and stop)

Other options

There are many Python image processing libraries, and for a particular purpose it may be best to use something other than Pillow. Examples:

  • OpenCV is targeted at computer vision and machine learning applications (e.g. face detection)
  • Scikit-image aims to be high-performance and to support video files. It uses numpy arrays extensively.

Generally, Pillow tends to emphasize minimal dependencies and doing basic things well.


GUI image editing tools are good for making modifications to an image that require planning, decisions, review, revision, etc.

PIL and other programmatic image manipulation libraries are great for batch operations and cases where the input or output of a program is naturally an image.


Revision history

  • 2022-03-07 Initial publication