[Python编程(第4版)].(Programming.Python.4th.Edition).Mark.Lutz.文字版

(yzsuai) #1

win.mainloop()
print(imgobj.width(), imgobj.height()) # show size in pixels on exit


With PIL, our script is now able to display many image types, including the default
JPEG image defined in the script and captured in Figure 8-44. Again, run with a
command-line argument to view other photos.


Figure 8-44. tkinter+PIL JPEG display


Displaying all images in a directory


While we’re at it, it’s not much extra work to allow viewing all images in a directory,
using some of the directory path tools we met in the first part of this book.
Example 8-44, for instance, simply opens a new Toplevel pop-up window for each
image in a directory (given as a command-line argument or a default), taking care to
skip nonimage files by catching exceptions—error messages are both printed and dis-
played in the bad file’s pop-up window.


Example 8-44. PP4E\Gui\PIL\viewer-dir.py


"""
display all images in a directory in pop-up windows
GIFs work in basic tkinter, but JPEGs will be skipped without PIL
"""


Viewing and Processing Images with PIL | 495
Free download pdf