Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I print values only once, instead of each single value?

I am trying to make a script that scans an image, and then tells me what color pixels are in it, but when I print the pixels color value, it prints the same color value for every time it occurs in the image.

I have figured out to sort the values sort they are besides each other.

from PIL import Image


img = Image.open('images/image.png')

print(img.size)
print("pixels")

pix = img.load()
pix_val = list(img.getdata())

pix_val.sort()

print(pix_val)

It prints the color values for each time they occur in the image, so I get a very long print.

like image 231
Galaxygon Avatar asked Nov 07 '22 14:11

Galaxygon


1 Answers

You can get the distribution of unique pixels using the following code:

from collections import Counter
Counter(pix_val)

If you only need to know which are the unique colours, just run the following code as Adrian told you on the comments.

set(pix_val)
like image 77
ivallesp Avatar answered Nov 14 '22 22:11

ivallesp