Search code examples
pythoncountrgba

Counting RGBA values with Python


I read an image, shoved the RGBA values into an array and now I want to count the occurence of certain colors. However all I'm getting is 0. How do I do that (without converting to string)? Relevant code snippets and output:

Output:

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

Code:

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)

Solution

  • The quotes are your problem here, you're searching for a tuple and not a string. Just leave the quotes and use

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