Image.getpixel method from Pillow library
- Install
Pillow
library from the command line:
pip install Pillow
- Count number of unique colors in image:
from PIL import Image
imgPath = 'test.jpg'
img = Image.open(imgPath)
uniqueColors = set()
w, h = img.size
for x in range(w):
for y in range(h):
pixel = img.getpixel((x, y))
uniqueColors.add(pixel)
totalUniqueColors = len(uniqueColors)
print(totalUniqueColors)
Leave a Comment
Cancel reply