2017-08-02 51 views
0

我有一个数据透视表数组,其中包含因子以及X和Y坐标(如下面的坐标),并且我有一个带有64种颜色的查找表,它们具有RGB值。我试图给每个因素组合分配一个颜色,我不太确定如何去做。例如,我需要所有A(0)B(1)C(0)D(0)为RGB值(1 0 103),这样我才可以在XY点将这些颜色绘制到图像上。为不同的值组合分配独特的颜色Python

A B C D Xpoint Ypoint 
0 1 0 0 20  20 
0 1 1 0 30  30 
0 1 0 0 40  40 
1 0 1 0 50  50 
1 0 1 0 60  60 

到目前为止,我只需要代码来打开我的两个LUT和数据透视表文件和代码,看数据透视表的长度。

import pandas as pd 
from PIL import Image, ImageDraw 

## load in LUT of 64 colours ## 
with open('LUT64.csv') as d: 
    LUT64 = pd.read_table(d, sep=',') 
    print LUT64 

## load in XY COordinates ## 
with open('PivotTable_2017-07-13_001.txt') as e: 
    PivotTable = pd.read_table(e, sep='\t') 
    print PivotTable 

## Bring in image ## 
IM = Image.open("mothTest.tif") 
IM.show() 

#bring in number of factors 
numFactors = 16  

#assign colour vectors to each factor combo 
numPTrows = len(PivotTable) 
print numPTrows 

#Apply colour dots to image at XY coordinates 

任何帮助将不胜感激!

回答

1

您可以使用您的颜色值dict与关键的表的前四个值(铸造成一个元组):

table = [ 
    [0, 1, 0, 0, 20, 20], 
    [0, 1, 1, 0, 30, 30], 
    [0, 1, 0, 0, 40, 40], 
    [1, 0, 1, 0, 50, 50], 
    [1, 0, 1, 0, 60, 60], 
] 

##generating some colors 
colors = [ (i,i,i) for i in range(0,256, 5)] 

##defining iterator over color table 
c_it = iter(colors) 

##the dictionary for the color values  
color_dict = dict() 

##assigning one color for each unique (A,B,C,D) tuple: 
for entry in table: 
    key = tuple(entry[0:4]) 

    if key not in color_dict: 
     color_dict[key] = next(c_it) 

print(color_dict) 

的这个输出是:

{ 
    (1, 0, 1, 0): (10, 10, 10), 
    (0, 1, 1, 0): (5, 5, 5), 
    (0, 1, 0, 0): (0, 0, 0) 
} 

编辑

在对应的OP的问题的编辑,这里是如何操纵你的Pillow Image(未经测试):

##looping through table: 
for entry in table: 
    key = tuple(entry[0:4]) 
    coord = tuple(entry[4:6]) 
    color = color_dict[key] 
    IM.putpixel(coord,color) 
+0

我已经有64种颜色选择(所以他们不看彼此相似),这就是被导入到一个数组中的文本文件。这是否意味着我不得不将我的LUT变成一张桌子(而不是像上面那样产生它们)?此外,我将在上表中使用X和Y坐标的这些颜色,那么仍然可以将字典中的颜色与XY坐标关联起来吗? – mdicrist

+0

@mdicrist如果没有看到你所有的代码都很难具体,但基本上你的问题的答案是肯定的,是的。如果你的数组中有你的颜色,你应该能够用这个数组替换我的“颜色”。然后,当你遍历你的'table'时,你首先选择坐标,然后按照他们的方式创建键。话虽如此,可能有一个更简单的解决方案来解决你的问题,但为此你必须显示你的代码。 –

+0

谢谢!我发布了我的代码。我之前没有发布它,因为我所做的只是将数据作为数组引入,并检查数据透视表(4072行)的长度。 – mdicrist

相关问题