How to reduce color palette with PIL

后端 未结 3 2027
天命终不由人
天命终不由人 2020-12-03 01:40

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 ov

相关标签:
3条回答
  • 2020-12-03 02:04

    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.

    0 讨论(0)
  • 2020-12-03 02:21

    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

    0 讨论(0)
  • 2020-12-03 02:21

    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 ?

    0 讨论(0)
提交回复
热议问题