This question seems to come up regularly - so here's a quick and dirty class
that should work for displaying in Tkinter (cross-platform, although I've
only tested on Windows).
Kevin.
----- Original Message -----
From: "Nand Rathi" <[EMAIL PROTECTED]>
To: "python_ImageSig" <[email protected]>
Sent: Saturday, December 03, 2005 2:47 PM
Subject: [Image-SIG] Please,I need help regarding displaying a PIL image in
realtime...
Hello All
I am new to PIL so I apolozize in advance if what I
ask is irrelevant to PIL. I am working with Python
2.4, PIL 1.1.5 in RH Linux and winXP environment.
My application is collecting realtime data from
multiple sensors. The data samples arrive on an
average every 6 to 8 seconds. I need to plot the
sensor data in realtime for viewing.
I can use PIL beautifully to create the Images etc of
the data and also can manipulate the image with the
fresh arival of sensor data. The problem is viewing or
displaying the image in realtime.
File based viewing is inappropraite as well as slow
here. Image.show method is also inappropriate as it
will spawn an external process every 6 seconds or so.
Too slow. In my opinion, what I require is a bitmapped
graphics capable of rendering a PIL image in realtime
with a refresh method which can be used to display the
fresh arrived sensor data. There is a mention of an
img package by Jack Jensen for X display in PIL
handbook, but I couldn't locate the package via
Google.
Please help me with the direction here. Is there any
other Python package which will fit the solution? I am
also open to consider an alternative solution, as long
as it will allow me realtime plotting of the data.
I appreciate any help or guidance in this regard.
regards
Nand
__________________________________________
Yahoo! DSL - Something to write home about.
Just $16.99/mo. or less.
dsl.yahoo.com
_______________________________________________
Image-SIG maillist - [email protected]
http://mail.python.org/mailman/listinfo/image-sig
# showTk.py
#
# by Kevin Cazabon, Dec 2005
# [EMAIL PROTECTED]
import Image, Tkinter, ImageTk
def sizeToMax(im, maxSize):
imAspect = float(image.size[0])/float(image.size[1])
outAspect = float(maxSize[0])/float(maxSize[1])
if imAspect >= outAspect:
#set to maxWidth x maxWidth/imAspect
return image.resize((maxSize[0], int((float(maxSize[0])/imAspect) +
0.5)), Image.BICUBIC)
else:
#set to maxHeight*imAspect x maxHeight
return image.resize((int((float(maxSize[1])*imAspect) + 0.5),
maxSize[1]), Image.BICUBIC)
class showTk:
def __init__(self, parent = None, im = None, maxSize = None):
# show an image in a Tk window. This can be part of a Tk GUI, or
stand-alone.
# parent = None or a Tk window that's already made
# im = a filename or an open PIL image
# maxSize = tuple of maximum horiz/vertical size for the display
# create the main window as a Tk() or TopLevel()
if parent == None:
self.main = Tkinter.Tk()
self.mainloopRequired = True
else:
self.main = Tkinter.Toplevel(parent)
self.mainloopRequired = False
self.maxSize = maxSize
self.label = Tkinter.Label(self.main)
self.label.pack()
if im != None:
self.update(im, maxSize)
def mainloop(self):
if self.mainloopRequired:
self.main.mainloop()
def update(self, im = None, maxSize = None):
# update the image to a new one
# im = a filename or an open PIL image, or None to blank it out
# maxSize = tuple of maximum horiz/vertical size for the display
# if a filename is passed in, open the image file
if type(im) == type("string"):
try:
im = Image.open(im)
except Exception, reason:
return False, "Could not open image: %s" %reason
# size the image if required - default to the maxSize given at init if
not over-ridden
if self.maxSize != None and maxSize == None:
maxSize = self.maxSize
if maxSize != None and im != None:
try:
im = sizeToMax(im, maxSize)
except Exception, reason:
return False, "Could not resize image to maxSize: %s" %reason
# delete the current image so there's no resource conflict
self.label.configure(image = None)
self.label.update_idletasks()
# update the display with a new image
if im != None:
try:
self.im = ImageTk.PhotoImage(im)
except Exception, reason:
return False, "Could not make PhotoImage of im: %s" %reason
self.label.configure(image = self.im)
self.label.update_idletasks()
return True, "Update OK"
if __name__ == "__main__":
import time
window = showTk(im = "c:\\temp\\test.tif")
time.sleep(5) # window will block during sleep, sorry
window.update(im = "c:\\temp\\test2.tif")
window.mainloop() # you have to call this if it's the main GUI window,
# so it enters the local event processing loop, but
# it's safe to call in any circumstance.
_______________________________________________
Image-SIG maillist - [email protected]
http://mail.python.org/mailman/listinfo/image-sig