我有一个带有因子和X和Y坐标的数据透视表数组,如下面的那个,我有一个64色的查找表,它有RGB值。我正在尝试为每个因素组合分配一种颜色,我不太清楚如何去做。例如,我需要所有A(0)B(1)C(0)D(0)为RGB值(1 0 103),以便我可以将这些颜色绘制到XY点处的图像上。
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
任何帮助将不胜感激!
答案 0 :(得分: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)