See All Titles |
![]() ![]() Manipulating ImagesPython 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:
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 LibraryThe 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.
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 ModulesBesides PIL, some other modules can help you manipulate graphic and image files. imghdr ModuleThis 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 ModuleThe 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 ModuleWBMP 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 ModuleOpenGL, 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.
|
Index terms contained in this sectionAscher, Davidattributes 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. |