Skip to content Skip to sidebar Skip to footer

Return Rgb Color Of Image Pixel Under Mouse Tkinter

I am trying to get the RGB values from where ever my mouse clicks in the an image I am trying to do this all with just Tkinter to keep the code simple (and for some reason I ca

Solution 1:

If you're using Python 2.5 or >, you can use the ctypes library to call a dll function that returns a color value of a pixel. Using Tkinter's x and y _root method, you can return the absolute value of a pixel, then check it with the GetPixel function. This was tested on Windows 7, Python 2.7:

from Tkinter import *
from ctypes import windll

root = Tk()

defclick(event):
    dc = windll.user32.GetDC(0)
    rgb = windll.gdi32.GetPixel(dc,event.x_root,event.y_root)
    r = rgb & 0xff
    g = (rgb >> 8) & 0xff
    b = (rgb >> 16) & 0xffprint r,g,b

for i in ['red', 'green', 'blue', 'black', 'white']:
    Label(root, width=30, background=i).pack()

root.bind('<Button-1>', click)

root.mainloop()

References:

Faster method of reading screen pixel in Python than PIL?

http://www.experts-exchange.com/Programming/Microsoft_Development/Q_22657547.html

Post a Comment for "Return Rgb Color Of Image Pixel Under Mouse Tkinter"