Getting pixel values under cursor

Jeff Epler jepler at
Mon Jun 17 16:52:53 CEST 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,, "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.bind("<Motion>", update_it)



More information about the Python-list mailing list