I have a CompSci project I designed where I wanted to make a program that can tell me the pixel information under the cursor, match it to a list of RGB values in a CSV file, find the closest color, and display it.
here is my CSV file
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
My goal is to use the min function to find the closest red value in my RGB value under the cursor. Then, to check the corresponding green values of the reds that matched. Then, check for the closest blue value for the matching red and green. Then, I can pull the color value and i will know what color the selected pixel is. My issue is that I have no idea on whether I should be turning my CSV data into lists, creating dictionaries, or what. I've tried everything and keep getting stuck. Based on what I said I want to do, can someone help me out, or point me in the right direction?
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])