Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从python中的特定列创建列表,等等_Python - Fatal编程技术网

如何从python中的特定列创建列表,等等

如何从python中的特定列创建列表,等等,python,Python,我设计了一个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,1

我设计了一个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])

将这些颜色视为三维空间中的点。您希望从这些已存在的点中找到离您最近的点。幸运的是,有一个既定的公式可以做到这一点。让我们从您的代码示例开始

from csv import *
首先,我们需要一些数学函数,所以让我们导入我们需要的:

from math import sqrt, pow
让我们添加一个变量来跟踪当前最接近的颜色。这将是一个2元素数组:文件中颜色的名称以及与请求点的距离

closestColor = []
在您的
myReader=reader(myFile)
声明之后,让我们先看一下标题(想法取自)

现在,在已定义的循环中,让我们计算文件中的点与已收集的点之间的距离:

lineRed = line[1]
lineGreen = line[2]
lineBlue = line[3]

distance = sqrt(pow(colorRed - lineRed, 2) + pow(colorGreen - lineGreen, 2) + pow (colorBlue - lineBlue, 2))
现在,让我们确定当前最接近的颜色是否比我们刚才计算的颜色更接近或更远

if distance < closestColor[1]:
    closestColor = [line[0], distance]
现在,在列表之外,你可以做你需要做的事情!因此,代码段的结尾应该是这样的(我假设您以前捕获颜色的代码已经过测试,并且是正确的):

#获取像素信息并将其与文本文件中的颜色匹配
从csv导入*
从数学导入sqrt,pow
closestColor=[]
打开(“Color Collection.csv”)作为我的文件:
myReader=读取器(myFile)
下一个(读卡器,无)
对于myReader中的行:
lineRed=行[1]
lineGreen=行[2]
lineBlue=行[3]
距离=sqrt(功率(红色-线红色,2)+\
功率(色绿色-线绿色,2)+\
pow(彩蓝-线蓝,2))
如果(len(closestColor)<2)或(距离#如果您必须使用创建一个函数,该函数将遍历您的颜色列表,并通过计算该颜色的接近程度百分比来查找最接近的颜色:选中此答案,以便这正是我所需要的。感谢您的深入和深思熟虑的解释——我能够理解并从中学习,所以我不仅仅是盲目复制代码(:我做了一些调整,将行变量转换为整数进行数学运算,并使用
titles=next(myReader)
而不是
next(reader,None)
。谢谢!
if distance < closestColor[1]:
    closestColor = [line[0], distance]
if (len(closestColor) < 2) or (distance < closestColor[1]):
    closestColor = [line[0], distance]
#Take pixel information and match it to a color in the text file
from csv import *
from math import sqrt, pow

closestColor = []
with open("Color Collection.csv") as myFile:
    myReader = reader(myFile)
    next(reader, None)
    for line in myReader:
        lineRed = line[1]
        lineGreen = line[2]
        lineBlue = line[3]

        distance = sqrt(pow(colorRed - lineRed, 2) + \
                        pow(colorGreen - lineGreen, 2) + \
                        pow(colorBlue - lineBlue, 2))

        if (len(closestColor) < 2) or (distance < closestColor[1]):
            closestColor = [line[0], distance]

# This style works on Python3.6+
print(f'The closest color to the point clicked is {closestColor[0]}')

# If you have to use <3.5, I'm sorry, but you can do this instead
# print('The closest color to the point clicked is %s', %(closestColor[0]))