简体   繁体   English

返回鼠标Tkinter下图像像素的RGB颜色

[英]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我试图从我的鼠标在图像中单击的位置获取 RGB 值

I am trying to do this all with just Tkinter to keep the code simple (and for some reason I can't install PIL correctly), and I don't know if it's possible.我试图只用 Tkinter 来完成这一切,以保持代码简单(由于某种原因,我无法正确安装 PIL),我不知道是否可能。 Thanks for any help, I'm stumped.感谢您的帮助,我很难过。

from serial import *
import Tkinter
class App:
    def __init__(self):
        # Set up the root window
        self.root = Tkinter.Tk()
        self.root.title("Color Select")

        # Useful in organization of the gui, but not used here
        #self.frame = Tkinter.Frame(self.root, width=640, height=256)
        #self.frame.bind("<Button-1>", self.click)
        #self.frame.pack()

        # LABEL allows either text or pictures to be placed
        self.image = Tkinter.PhotoImage(file = "hsv.ppm")
        self.label = Tkinter.Label(self.root, image = self.image)
        self.label.image = self.image #keep a reference see link 1 below

        # Setup a mouse event and BIND to label
        self.label.bind("<Button-1>", self.click)
        self.label.pack()
        # Setup Tkniter's main loop
        self.root.mainloop()

    def click(self, event):
        print("Clicked at: ", event.x, event.y)

if __name__ == "__main__":
    App()

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.如果您使用的是 Python 2.5 或 >,您可以使用ctypes库来调用一个返回像素颜色值的 dll 函数。 Using Tkinter's x and y _root method, you can return the absolute value of a pixel, then check it with the GetPixel function.使用 Tkinter 的 x 和 y _root 方法,您可以返回像素的绝对值,然后使用 GetPixel 函数进行检查。 This was tested on Windows 7, Python 2.7:这是在 Windows 7、Python 2.7 上测试的:

from Tkinter import *
from ctypes import windll

root = Tk()

def click(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) & 0xff
    print 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? 在 Python 中读取屏幕像素比 PIL 更快的方法?

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

Well I broke down and installed PIL.好吧,我崩溃了并安装了 PIL。 I was able to get the pixel collection working, however now I have trouble sending the serial...我能够使像素集合正常工作,但是现在我无法发送串行...

 def click(self, event):
    im = Image.open("hsv.ppm")
    rgbIm = im.convert("RGB")
    r,g,b = rgbIm.getpixel((event.x, event.y))
    colors = "%d,%d,%d\n" %(int(r),int(g),int(b))
    #print("Clicked at: ", event.x, event.y)
# Establish port and baud rate
    serialPort = "/dev/ttyACM0"
    baudRate = 9600
    ser = Serial(serialPort, baudRate, timeout = 0, writeTimeout = 0)
    ser.write(colors) 
    print colors

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM