Getting pixel values under cursor
Jeff Epler
jepler at unpythonic.net
Mon Jun 17 10:52:53 EDT 2002
I don't know that Tk offers the general capability to find the color
under a pixel. However, it is possible to find the color at a
particular coordinate within a photo image. In tcl/tk,
"<image name> get x y" returns the RGB triple .. This doesn't seem to be
provided in Tkinter.Image, so I derive a class here.
from Tkinter import *
class ImagePlus(Image):
def get(self, x, y):
return tuple(map(int, self.tk.splitlist(
self.tk.call(self.name, "get", x, y))))
def update_it(evt):
x = evt.x
y = evt.y
rgb = p.get(x,y)
print `rgb`
l.configure(text="#%02x%02x%02x" % rgb)
t = Tk()
p = ImagePlus("photo", master=t, file="dilbert2001114622012.gif")
l = Label(t)
x = Label(t)
x.configure(image=p)
x.bind("<Motion>", update_it)
l.pack()
x.pack()
t.mainloop()
Jeff
More information about the Python-list
mailing list