【发布时间】:2020-04-20 19:23:21
【问题描述】:
我有一个我设计的 CompSci 项目,我想制作一个程序,它可以告诉我光标下的像素信息,将其与 CSV 文件中的 RGB 值列表匹配,找到最接近的颜色并显示它。
这是我的 CSV 文件
Color,Red,Green,Blue
Black,0,0,0
White,255,255,255
Red,255,0,0
Lime,0,255,0
Blue,0,0,255
Yellow,255,255,0
Cyan,0,255,255
Magenta,255,0,255
Silver,192,192,192
Gray,128,128,128
Maroon,128,0,0
Olive,128,128,0
Green,0,128,0
Purple,128,0,128
Teal,0,128,128
Navy,0,0,128
我的目标是使用 min 函数在光标下的 RGB 值中找到最接近的红色值。然后,检查匹配的红色的相应绿色值。然后,检查匹配的红色和绿色最接近的蓝色值。然后,我可以提取颜色值,我会知道所选像素是什么颜色。我的问题是我不知道是否应该将我的 CSV 数据转换为列表、创建字典或其他什么。我已经尝试了一切并且一直卡住。根据我所说的我想做的事情,有人可以帮助我,或者指出我正确的方向吗?
from image import *
from PIL import *
from pynput.mouse import Listener
import PIL.ImageGrab
#Create an imagewindow based on image dimensions; display the image
def printImage(imageFile):
myImage = FileImage(imageFile)
_width = myImage.getWidth()
_height = myImage.getHeight()
myWindow = ImageWin(_height, _width, "window")
myImage.draw(myWindow)
printImage("mickey.png")
#Color finding function
def getColor(_x, _y):
return PIL.ImageGrab.grab().load()[_x, _y]
#Uses mouse position for x and y value of color finding function
def on_move(x, y):
global _color
_x = x
_y = y
_color = getColor(_x, _y)
print(_color)
def on_click(x, y, button, pressed):
print('done')
if not pressed:
return False
#allows on move to be updated every time it occurs
with Listener(on_move=on_move, on_click=on_click) as listener:
listener.join()
colorRed = _color[0]
colorGreen = _color[1]
colorBlue = _color[2]
#Take pixel information and match it to a color in the text file
from csv import *
with open("Color Collection.csv") as myFile:
myReader = reader(myFile)
for line in myReader:
print(line[1])
【问题讨论】:
-
创建一个函数,该函数将遍历您的颜色列表,并通过计算该颜色的接近百分比来找到最接近的颜色:how to calculate percentage
-
在 SO https://stackoverflow.com/a/12070940/3210415 上查看此答案
标签: python