2016-04-06 62 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))