How to reduce color palette with PIL

隐身守侯 提交于 2019-11-27 12:53:11

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

tzot

The short answer is to use the Image.quantize method. For more info, see: How do I convert any image to a 4-color paletted image using the Python Imaging Library ?

Paul

I assume you want to do something more sophisticated than posterize. "Sampling" as you say, will take some finesse, as the 5 most common colors in the image are likely to be similar to one another. Maybe take a look at the 5 most separated peaks in a histogram.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!