< BACKMake Note | BookmarkCONTINUE >
152015024128143245168232148039199167010047123209178152124239215162148044238004005252068155

Manipulating Images

Python has comprehensive support for handling image files. The foundation of this structure is based on the Python Imaging Library (commonly known as PIL).

PIL is a set of Python modules that compound an extensive framework written by Fredrik Lundh, from Secret Labs AB. PIL is able to convert and manipulate image files of several different formats (such as GIF, JPEG, and PNG), and provides powerful graphics capabilities. Its framework is cross-platform, which allows it to perform image manipulation and processing in different systems using the same code. PIL also supports some Windows-specific extensions that enable it to display images using the Windows API.

Some of the main features of PIL are summarized in the following:

  • PIL can load image objects from a variety of formats.

  • It enables the Python interpreter to handle image objects.

  • PIL enables a rich set of image operations to be applied to those objects.

  • It saves images files to disk.

  • It uses graphical interfaces, such as Tkinter and PythonWin in order to show the resulting images.

  • It allows you to create thumbnails automatically from a collection of images.

  • You can create, read, and write different images formats, including JPEG, GIF, and TIFF.

  • It provides supports to some animation formats, such as FLI and MPEG.

  • It automatically identifies file formats.

  • PIL can be used to make file conversions between graphic files of different formats.

  • PIL also handles changes in the image file's color table (for example, it can change the color table of a file from RGB to grayscale).

These are just some of things you can do with PIL. You are invited to create an image object in the interpreter using PIL, and play around for a while.

PIL's home page and download center is located at the following site:

http://www.pythonware.com/products/pil/index.htm

Similar to Python itself, PIL is copyrighted but can be used without fee.

Python Imaging Library

The Image class is the most important class of PIL. To use it, you need to import the Image module, and launch the open method. This method is very fast because it doesn't decode the whole image. It just reads the image header in order to start working with the file.

						
>>> import Image
>>> im = Image.open("c:\\logo.gif")

					

As you can see in the next example, you can also load an image (GIF or JPEG) straight from a URL without saving it to a file first. Note that filelocation is any file handle like python object.

						
>>> filename = "http://www.lessaworld.com/images/brazil.gif "
>>> filelocation = urllib.urlopen(filename)
>>> im = Image.open(filelocation)
				
					

Every image object that is created by the open function exposes three attributes: format, size, and mode.

im.format—   Identifies the source format of the image.

im.size—   It is a 2-tuple variable that contains the image's width and height.

im.mode—Provides the image mode, such as grayscale (L), CMYK, or RGB mode. The attribute called Image.MODES lists all the modes supported by the library.

								
>>> print im.format, im.size, im.mode
GIF (200, 130) L

							

If you want to generate a thumbnail image, you need to call the thumbnail method and provide the size of the new image. Note that a new object isn't created because the change is applied to the old object. Therefore, the image must be copied if you need both the original and thumbnail images.

						
>>> im.thumbnail((50, 32))

					

After you have done everything that you need, you can think about saving the new file. Notice that the first argument in the save method is the name of the output file, and the second argument is the format to be saved. If the format argument is omitted, the format is deduced from the file extension.

						
>>> outfile = "a:\\out.jpg"
>>> im.save(outfile, "JPEG")

					

Many other methods can be applied on the image. For cutting, pasting, and merging images, you can use im.crop(), im.paste(), and im.transpose(). For resizing and rotating an image, im.resize() and im.rotate() are available.

For a complete tutorial about using PIL, check out the Python Imaging Library Handbook at the following site:

						
					http://www.pythonware.com/library/pil/handbook/index.htm
				
					

Other Modules

Besides PIL, some other modules can help you manipulate graphic and image files.

imghdr Module

This module recognizes image files based on their headers'first few bytes. The imghdr module is part of the standard distribution. This module implements the what() function, which returns the file type.

							
>>> import imghdr
>>> imgfile = imghdr.what("d:\\logo.gif")
>>> print imgfile
gif

						

The file types currently supported are: SGI image library, GIF ('87 and '89 variants), PBM (portable bitmap), PGM (portable graymap), PPM (portable pixmap), TIFF (can be in Motorola or Intel byte order), Sun raster file, X bitmap (X10 or X11), JPEG data in JFIF format, BMP, and PNG.

GD Module

The GD module is an interface to the GD GIF library that allows your code to quickly draw images complete with lines, arcs, text, multiple colors, cut and paste from other images, and flood fills, and to write out the result as a .GIF file. This module is currently no longer maintained. Newer gd libraries generate png images rather than gifs. Also, GD is not Free Software as it has commercial use restrictions. For more information, check out the following site:

							
						http://starship.beopen.com/crew/richard/gdmodule/
					
						
WBMP Module

WBMP is a wireless bitmap format, the graphic format used by WAP mobile phones. A WBMP module for PIL is available for download at http://www.rcp.co.uk/ distributed/Downloads

The filename is wbmpconvsrc.zip. The download includes a script for converting between WBMP and any other PIL supported bitmap format.

PyOpenGL Module

OpenGL, created by Silicon Graphics, is a portable library for rendering. It is a complex API with superior performance that became an industry standard for 2D and 3D graphics.

The Open GL home page is located at http://www.opengl.org.

PyOpenGL is a wrapper class for the OpenGL library that is maintained by David Ascher. It can be found at http://starship.python.net/crew/da/PyOpenGL.


Last updated on 1/30/2002
Python Developer's Handbook, © 2002 Sams Publishing

< BACKMake Note | BookmarkCONTINUE >

Index terms contained in this section

Ascher, David
attributes
      im.mode
classes
      Image
files
     image
            saving
functions
      what()
generating
      thumbnail images
graphics
      manipulating 2nd 3rd
im.mode attribute
Image class
Image module
images
      manipulating 2nd 3rd
imghdr module
libraries
      Python Imaging
loading
      images
manipulating
      images 2nd 3rd
methods
      open
modules
      Image
      imghdr
      OpenGL
open method
OpenGL module
PILÓ
      Ò
Python Imaging Library
saving
      images
Silicon Graphics
      OpenGL module
thumbnail images
      generating
what() function

© 2002, O'Reilly & Associates, Inc.