Lecture 20

Pillow

MCS 275 Spring 2024
Emily Dumas

View as:   Presentation   ·   PDF-exportable  ·   Printable

Lecture 20: Pillow

Reminders and announcements:

  • Project 1 graded
  • Project 2 due 11:59pm tonight
  • Homework 7 posted

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 is the name of the package (what you ask pip to install).

The module you import to use it is called PIL.

The name difference is for historical reasons.

Pillow offers a drop-in replacement for a different module called PIL, "Python Imaging Module", that was abandoned in 2011.

Images

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 with recursion, iteration, etc.)

Bitmap images

A rectangular grid of color values, meant* to be displayed with each one 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

* usually; high resolution bitmap on a lower-resolution display looks ok

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  # just import PIL is not enough!

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

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

Modes

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" - 8 bits each for red, green, blue.
  • "RGBA" - 32 bits per pixel, RGB plus 8-bit transparency value (0 = fully transparent)

RGB and RGBA are the most common. There are lots of other modes modes not listed here.

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) )

Operations

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.


        frame0.save(
            "anim.gif",
            save_all=True,
            append_images=[frame1,frame2,...],
            duration=50,
            loop=0
        )
    

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.

Strengths

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.

References

Revision history

  • 2023-03-03 Finalization of the 2023 lecture this was based on
  • 2024-02-23 Initial publication