straightforward: given a directory of image files, PyPhoto displays their thumbnails in
a scrollable canvas. When a thumbnail is selected, the corresponding image is displayed
full size in a pop-up window.
Unlike our prior viewers, though, PyPhoto is clever enough to scroll (rather than crop)
images too large for the physical display. Moreover, PyPhoto introduces the notion of
image resizing—it supports mouse and keyboard events that resize the image to one of
the display’s dimensions and zoom the image in and out. Once images are opened, the
resizing logic allows images to be grown or shrunk arbitrarily, which is especially handy
for images produced by a digital camera that may be too large to view all at once.
As added touches, PyPhoto also allows the image to be saved in a file (possibly after
being resized), and it allows image directories to be selected and opened in the GUI
itself, instead of just as command-line arguments.
Put together, PyPhoto’s features make it an image-processing program, albeit one with
a currently small set of processing tools. I encourage you to experiment with adding
new features of your own; once you get the hang of the Python Imaging Library (PIL)
API, the object-oriented nature of PyPhoto makes adding new tools remarkably simple.
Running PyPhoto
In order to run PyPhoto, you’ll need to fetch and install the PIL extension package
described in Chapter 8. PyPhoto inherits much of its functionality from PIL—PIL is
used to support extra image types beyond those handled by standard tkinter (e.g., JPEG
images) and to perform image-processing operations such as resizes, thumbnail crea-
tion, and saves. PIL is open source like Python, but it is not presently part of the Python
standard library. Search the Web for PIL’s location (http://www.pythonware.com is
currently a safe bet). Also check the Extensions directory of the examples distribution
package for a PIL self-installer.
The best way to get a feel for PyPhoto is to run it live on your own machine to see how
images are scrolled and resized. Here, we’ll present a few screenshots to give the general
flavor of the interaction. You can start PyPhoto by clicking its icon, or you can start it
from the command line. When run directly, it opens the images subdirectory in its
source directory, which contains a handful of photos. When you run it from the com-
mand line, you can pass in an initial image directory name as a command-line argument.
Figure 11-7 captures the main thumbnail window when run directly.
Internally, PyPhoto is loading or creating thumbnail images before this window ap-
pears, using tools coded in Chapter 8. Startup may take a few seconds the first time
you open a directory, but it is quick thereafter—PyPhoto caches thumbnails in a local
subdirectory so that it can skip the generation step the next time the directory is opened.
Technically, there are three different ways PyPhoto may start up: viewing an explicit
directory listed on the command line; viewing the default images directory when no
command-line argument is given and when images is present where the program is run;
PyPhoto: An Image Viewer and Resizer | 717