Python and the Image Manipulation Library

Author: Jeff Rush
Copyright: 2007 Tau Productions Inc.
License:GNU Free Documentation License
Date: August 10, 2007
Version: 1
Series:5-Minutes with Python

An introduction to a widely used 2-d image manipulation package for Python named 'PIL', providing for the viewing, copying, printing filtering and transformation of images, including palette manipulation, pixel filtering and multipage animations.

Hello, my name is Jeff Rush and this talk is part of the "5-Minutes with Python" series. In this brief slideshow I'll provide a survey of the capabilities of the Imaging Library for Python. Although not part of the Python standard library, it is a commonly used component and illustrates the richness of modules available for Python.

Roadmap to Talk

  • What is PIL and why should I use it?
  • Loading, viewing, saving, printing and grabbing images
  • Transformations of shape, content and color
  • Drawing into and filtering images
  • Operating on multiple images
  • Get PIL and find the community

➔ In this talk I'll highlight the major features of the Imaging Library and why those may be useful to you.

➔ I'll cover the movement of images into and out of your programs along several avenues, and

➔ facilities for transforming images by shape, pixel content or colorspace, and

➔ speak a bit about drawing onto images and filtering images using certain mathematical processes.

➔ The Imaging Library can also be used to operate on complex images, such as multipage documents, page-flip animations or compositing one image onto another.

➔ I'll conclude with where to get the Imaging Library and where the Python imaging community hangs out.

What is PIL?

  • Python Imaging Library
  • 2-D rasters, not 3-D, not vector
  • reads many formats, writes few
  • more than RGB (CMYK, YCbCr)
  • supports alpha channel
  • cross-platform

First let's get the name straight.

PIL is the nickname for the Python Imaging Library but is referenced in your programs as the module Image.

➔ PIL provides for the manipulation of 2-dimensional raster or pixel-based images, not 3-dimensional nor vector graphics, although we'll see with its postscript support some aspects of vector graphics.

➔ PIL identifies and reads many different image formats and intentionally writes only the more common interchange and presentation formats.

➔ And it understands images constructed out of more than your traditional RGB components, such as CMYK four-color model and YCbCr luma/chroma components.

➔ PIL allows for the use of an alpha channel, giving you variable control over transparency.

➔ And it runs under multiple operating systems, giving you a platform-independent image manipulation capability.

Why use it?

  • efficient internal representation

  • usable with GUI toolkits

  • useful for archiving
    • image i/o
    • basic ops
    • multipage images

➔ PIL provides an efficient internal representation of multiple image formats, providing fast operation without having to resort to a compiled programming language,

➔ It has hooks to interoperate with several different GUI frameworks, including wxWindows and Tkinter.

➔ PIL has features that make it especially suitable to the archiving of images, such as

➔ I/O to not just files but also containers such as tarfiles,

➔ basic image operations suitable for batch processing, and

➔ support for multipage images such as scanned documents.

I/O: Loading, Saving, Viewing Images

  • loading
    • individual files with auto-id
    • members of archive containers
  • saving
    • automatic format conversion
  • viewing
    • save and invoke program
    • GUI frameworks: Tkinter, wxWindows

PIL provides several ways of getting images into and out of your program.

➔ Besides ➔ reading from files (➔ with automatic format identification), it supports ➔ reading from archive containers such as tar files.

➔ And when saving out an image, PIL will ➔ automatically convert it to the destination format.

➔ For quickly viewing an image during development, PIL can ➔ save it to disk and run a viewer program. ➔ And you can embed your images in various other GUI frameworks.

I/O: Printing, Grabbing

  • printing
    • postscript
    • PDF
  • screen capture
    • from clipboard
    • from scanner via SANE

➔ PIL provides for the writing of images and other drawing elements into ➔ postscript and ➔ PDF files for printing.

Besides file I/O ➔ PIL can acquire images from the ➔ desktop clipboard and ➔ from a scanner device compatible with the SANE scanner interface standard.

Geometric Manipulation

  • resizing, thumbnails
  • flipping, rotating, transforming
  • cropping, cutting, pasting

PIL supports a complete set of geometric manipulations of images, such as ➔ resizing, perhaps to provide thumbnail images, and ➔ flipping them left-right, top-to-bottom, ➔ rotating by any number of degrees, and ➔ transforming them with various algorithms.

PIL makes it easy to ➔ crop images and ➔ cut, copy and paste pieces among images, while autoconverting the format of the pieces as needed.

Pixel/Colorspace Operations

  • point operations
  • convolutions (blur, emboss, smooth, etc.)
  • color space conversions
  • stats re histograms

Sometimes you need to apply operations to each pixel or group of pixels. ➔ PIL provides for point operations -- where each pixel is run through a value map, either a table or function.

For group operations, ➔ PIL comes with convolution kernels for blurring, sharpening, embossing and so forth.

➔ With PIL you have control over the color space, able to manipulate the palette, increase or decrease the number of colors or replace it entirely.

➔ And PIL gives you methods to extract, from all or part of your image, statistics such as mean, sum, count, standard deviation. Returned as a histogram, these methods are useful for automatic contrast enhancement and global statistical analysis.

Drawing into Images

  • what can be drawn?
    • images
    • text of various fonts (bitmap or truetype)
    • graphical elements (points, lines, etc.)
  • where can it be drawn?
    • onto bitmaps
    • onto postscript

PIL lets you draw onto images, whether annotating or retouching existing images, or generating graphics on the fly for web use. The first aspect to consider is ➔ what can be drawn onto an image.

Certainly ➔ images can be placed onto other images but ➔ also text, with control over the font used, as well as ➔ geometric elements such as points, lines, boxes and curves.

And the second aspect is ➔ where can we draw these? ➔ Onto other bitmaps of course, but also ➔ onto postscript pages, in which case they are vector or path-based, and resolution independent.

Image Sequencing

  • frame iteration
  • FLI/FLC, GIF animations
  • multipage TIFF

Although not a comprehensive animation tool, ➔ PIL implements seek and tell methods on ➔ those image formats ➔ that support multiple images.

With PIL you can take a sequence of images apart, operate on them and put them back together again.

For Further Reading

  • Main PIL website: http://www.pythonware.com/products/pil/

  • Python Image SIG mailing list http://mail.python.org/mailman/listinfo/image-sig

  • Contact me:

    Jeff Rush (Python Advocacy Coordinator) <advocate@python.org>

➔ ➔ PIL is an open source product of Secret Labs AB, under the banner of PythonWare. Besides being free, they also offer commercial support contracts.

➔ ➔ Within the Python community, those interested in imaging hang out on the mailing list of the Image special interest group

➔ ➔ ➔ And if you have any comments or suggestions about this talk, you can email me at "advocate" at "python dot org". We are looking for speakers to record other talks, 5-minutes or longer.

These slides and script are available under the Creative Commons Attribute-ShareAlike license, for use in your own presentations.