Monday, April 11, 2011

Python image display

PIL has a weaker way to display images since it calls xv and there is no interaction possible with the image window.

I found a neat code example with Tkinter and have modified that to get image ground truth.




import os, sys
import Image, ImageTk
import Tkinter

filename = "test.dat"
FILE = open(filename,"a")

def write_to_file(txt):
FILE.write(txt)

def button_click_exit_mainloop (event):
write_to_file('\t'+event.char)
event.widget.quit() # this will cause mainloop to unblock.


root = Tkinter.Tk()
root.bind("", button_click_exit_mainloop)
root.geometry('+%d+%d' % (100,100))


dirlist = os.listdir('.')
old_label_image = None
for f in dirlist:
try:
image1 = Image.open(f)
write_to_file(f)
root.geometry('%dx%d' % (image1.size[0],image1.size[1]))
tkpi = ImageTk.PhotoImage(image1)
label_image = Tkinter.Label(root, image=tkpi)
label_image.place(x=0,y=0,width=image1.size[0],height=image1.size[1])
root.title(f)
if old_label_image is not None:
old_label_image.destroy()
old_label_image = label_image
root.mainloop() # wait until user clicks the window
except Exception, e:
pass
FILE.close()

No comments: