返回鼠标Tkinter下图像像素的RGB颜色 [英] Return RGB Color of Image Pixel under Mouse Tkinter

查看:43
本文介绍了返回鼠标Tkinter下图像像素的RGB颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从鼠标在图像中点击的位置获取 RGB 值

I am trying to get the RGB values from where ever my mouse clicks in the an image

我试图只用 Tkinter 来完成这一切,以保持代码简单(由于某种原因,我无法正确安装 PIL),我不知道这是否可能.感谢您的帮助,我很难过.

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. 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()

推荐答案

好吧,我崩溃并安装了 PIL.我能够使像素集合正常工作,但是现在我无法发送串行...

Well I broke down and installed 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

这篇关于返回鼠标Tkinter下图像像素的RGB颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆