|
|
||||||||||||||||||
|
|
||||||||||||||||||
![]() |
![]() |
Issue 1 - Revision 1 / Published in 2002
|
|||
|
Image Viewing Module with Tkinter - - - - - - - - - - - - By Blake Garretson | Originally published in Py Issue 1 The one Python snippet that I reuse the most frequently is a small nine-line image viewer that I wrote. It is often useful to have the ability to display an image, even if you are just writing a command line program. In the past, when I wrote programs that produced graphs or pictures, I had to rely on external software to view the result. After overcoming my fears about GUI programming, I now find that it is much more convenient to handle pictures right in my own code. You do not have to be a Tkinter expert in order to piece together a very simple image viewer. Here is the code for a basic viewer function:
### viewimage.py
from Tkinter import *
def viewimage(filename):
root = Tk()
root.title("View Image")
frame = Frame(root, colormap="new", visual='truecolor').pack()
imagedata = PhotoImage(file=filename)
label = Label(frame, image=imagedata).pack()
quitbutton = Button(root, text="Quit", command=root.destroy).pack(fill=X)
root.mainloop()
This handful of lines creates a new window called "root", adds a frame, places a label containing the image inside of the frame, and then puts a quit button at the bottom of the window.
Most of the code is self-explanatory, but a few important notes should be made. First, you must have Tkinter correctly installed and configured. (I won't get into the details here, but it should have been done for you if you are using any of the modern Python installations on Linux or Windows.). Secondly, you may have noticed the " Finally, a note about file formats. The standard Tkinter package only handles a few image formats. The supported formats are GIF (with and without transparency), grayscale PGM, and truecolor PPM. If you want to use any other formats, you will need to install the Python Imaging Library (PIL) available at www.pythonware.com. PIL adds the ability to read many common formats such as JPEG, PNG, BMP, TIFF, and many others. Using PIL is not anymore difficult than using Tkinter by itself. In the revised program below, I just call PIL's PhotoImage method instead of Tkinter's built-in one.
### viewimage.py
from Tkinter import *
#import PIL
import ImageTk
def viewimage(filename):
root = Tk()
root.title("View Image")
frame = Frame(root,colormap="new",visual='truecolor').pack()
imagedata = ImageTk.PhotoImage(file=filename) # Call PIL's PhotoImage function
label = Label(frame,image=imagedata).pack()
quitbutton = Button(root,text="Quit",command=root.destroy).pack(fill=X)
root.mainloop()
###
Now the function will display any of the formats supported by PIL. You can get the full list of supported formats and the rest of the documentation at the Pythonware website.
Now let's try our new function in the context of a semi-useful program. I like to be on top of the weather, so I often view radar images that I get from the web. While manually surfing to the website will work, it becomes rather tedious if I do so several times a day. Why not let Python do the work for us? Go to a weather site such as www.accuweather.com and navigate to the radar image you want. Right click on the image to get the URL and plug it into the program below. This program uses the
### weathermap.py
import os, urllib, viewimage
RADAR_URL = “http://sirocco.accuweather.com/nxss_R1_640x480d/R1/inxR1kptks.gif"
mappic = urllib.urlretrieve( RADAR_URL,"delete-me.gif")
urllib.urlcleanup()
viewimage.viewimage("delete-me.gif")
os.remove("delete-me.gif")
root = Tk()
root.title("View Image")
frame = Frame(root,colormap="new",visual='truecolor').pack()
imagedata = ImageTk.PhotoImage(data=imagestring)
label = Label(frame,image=imagedata).pack()
quitbutton = Button(root,text="Quit",command=root.destroy).pack(fill=X)
root.mainloop()
RADAR_URL = "http://sirocco.accuweather.com/nxss_R1_640x480d/R1/inxR1kptks.gif"
mappic = urllib.urlopen(RADAR_URL)
mapstring = mappic.read()
urllib.urlcleanup()
viewimage(mapstring)
This program highlights a limitation with our viewimage() function. In weathermap.py, we need to save an image file, just to delete it a few lines later. What a waste of IO. Lucky for us, the PhotoImage methods of Tkinter and PIL will accept files or raw data as input. In the next example program, we will dump the contents of the image directly into a variable instead of saving it to a file. Then we will view it with a modified version of viewimage.
The main difference in the
The limits of If you are using external viewers in your programs, take the plunge and try rolling your own. The extra versatility that you will gain will be well worth the investment in time. You will also be filled with the satisfaction of running a 100% Python program that stands on its own two feet. This particular article is Copyright © 2002 Blake Garretson. All Rights Reserved.
|
|||||||||||||||||||||||||||||||||||||||||||||
|
Py is committed to bringing you great Python Articles. | ||||||||||||||||||||||||||||||||||||||||||||||
![]() |
Reproduction of material from any of PyZine's pages without prior written permission is strictly prohibited. Copyright 2003 - 2005 PyZine |
|