wxPython vs Tkinter for imaging front end GUI???

Fredrik Lundh fredrik at pythonware.com
Tue Jun 3 08:12:19 EDT 2003


Leonard J. Reder wrote:

> I have a command line image feature recognition code and I want to put a
> quick python script on it to execute it and give it a GUI.  I need to
> read in an image (JPEG), rescale the image on a canvas sort of display,
> place mouse over it to select some coordinate points.  And then just do
> other stuff with buttons and entries.  I am confortable with TK from
> using tcl but in Tk there is not an easy way to handle scaled images on
> the canvas.  Does wxPython have this sort of functionality included?

> Has this sort of thing been done with Tkinter and PIL in Python.

yes, all the time.

here's a minimal example (run as "python script.py imagefile"):

import sys
from PIL import Image, ImageTk
from Tkinter import Tk, Canvas, Button

root = Tk()
root.title("select region")

maxsize = 200, 200

im = Image.open(sys.argv[1])
truesize = im.size
im.thumbnail(maxsize)

print "scale is", float(truesize[0]) / im.size[0],
print "image pixels per screen pixel"

photo = ImageTk.PhotoImage(im)

canvas = Canvas(root, width=im.size[0], height=im.size[0], bd=0)
canvas.xview_moveto(0) # reset coordinate system
canvas.yview_moveto(0)
canvas.create_image(0, 0, image=photo, anchor="nw")
canvas.pack()

def click(event):
    x = event.widget.canvasx(event.x)
    y = event.widget.canvasy(event.y)
    x = x * truesize[0] / im.size[0]
    y = y * truesize[1] / im.size[1]
    print "clicked at original image pixel", x, y

canvas.bind("<Button-1>", click)

Button(root, text="done", command=root.quit).pack()

root.mainloop()

for more info, check the documentation for PIL's ImageTk
module.

</F>








More information about the Python-list mailing list