2016-04-06 61 views
0

我讀取圖像,將RGBA值推入數組中,現在我要計算某些顏色的出現次數。然而,我得到的是0.我怎麼做(不轉換爲字符串)?相關代碼段和輸出:使用Python計算RGBA值

輸出:

Image123.png 
8820 
[(138, 18, 20, 255), (138, 18, 20, 255), (138, 18, 20, 255), (138, 18, 20, 255), (138, 18, 20, 255), (138, 18, 20, 255), (138, 18, 20, 255), (138, 18, 20, 255), (138, 18, 20, 255), (138, 18, 20, 255), (138, 18, 20, 255), (138, 18, 20, 255), (138, 18, 20, 255), (138, 18, 20, 255), (138, 18, 20, 255), (138, 18, 20, 255), (138, 18, 20, 255), (138, 18, 20, 255), (138, 18, 20, 255), ...... 
0 
0 

代碼:

read_pixel = [] 

print(filename) 
read_pixel.append(pixel[image_x, image_y]) 

print(image_size_x*image_size_y) 
print(read_pixel) 

count_lte_70_1 = read_pixel.count("(138, 18, 20, 255)") 
print(count_lte_70_1) 

#without parenthesis 
count_lte_70_2 = read_pixel.count("138, 18, 20, 255") 
print(count_lte_70_2) 

回答

1

引號是你的問題在這裏,你正在尋找一個元組而不是一個字符串。只需留下引號並使用

read_pixel.count((138, 18, 20, 255)) 
1

嘛,你不應該使用count("(a,b,c,d)")count((a,b,c,d))

你現在的樣子做,現在計數列表中的字符串數量

x=[(1,2),(3,4),(3,4)] 
print(x.count((1,2)) #returns 1 
print(x.count((3,4)) #returns 2 
2

隨着

count_lte_70_1 = read_pixel.count("(138, 18, 20, 255)") 

你正在尋找一個的出現,而你的列表中包含元組。相反,你應該使用:

count_lte_70_1 = read_pixel.count((138, 18, 20, 255))