How to reduce color palette with PIL

Andrew picture Andrew · Jun 30, 2009 · Viewed 20.5k times · Source

I'm not sure how I would go about reducing the color palette of a PIL Image. I would like to reduce an image's palette to the 5 prominent colors found in that image. My overall goal is to do some basic color sampling.

Answer

Nadia Alramli picture Nadia Alramli · Jul 2, 2009

That's easy, just use the undocumented colors argument:

result = image.convert('P', palette=Image.ADAPTIVE, colors=5)

I'm using Image.ADAPTIVE to avoid dithering