Viewing and Processing Images with PIL
As mentioned earlier, Python Tkinter scripts show images by
associating independently created image objects with real widget
objects. At this writing, Tkinter GUIs can display photo image files
in GIF, PPM, and PGM formats by creating a PhotoImage
object, as well as X11-style
bitmap files (usually suffixed with an .xbm
extension) by creating a BitmapImage
object.
This set of supported file formats is limited by the underlying Tk library, not by Tkinter itself, and may expand in the future. But if you want to display files in other formats today (e.g., the popular JPEG format), you can either convert your files to one of the supported formats with an image-processing program, or install the PIL Python extension package mentioned at the start of Chapter 8.
PIL, the Python Imaging Library, is an open source system that supports nearly 30 graphics file formats (including GIF, JPEG, TIFF, and BMP). In addition to allowing your scripts to display a much wider variety of image types than standard Tkinter, PIL also provides tools for image processing, including geometric transforms, thumbnail creation, format conversions, and much more.
PIL Basics
To use its tools, you must first fetch and install the
PIL package: see http://www.pythonware.com (or
search for “PIL” on Google). Then, simply use special PhotoImage
and BitmapImage
objects imported from the PIL
ImageTk
module to open files in other graphic formats. These are compatible replacements for the ...
Get Programming Python, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.