2017-08-02 46 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

相關問題