如何使用 PIL 减少调色板

发布于 2024-07-26 09:27:27 字数 78 浏览 4 评论 0原文

我不确定如何减少 PIL 图像的调色板。 我想将图像的调色板减少到该图像中发现的 5 种突出颜色。 我的总体目标是进行一些基本的颜色采样。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

丢了幸福的猪 2024-08-02 09:27:27

这很简单,只需使用未记录的颜色参数:

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

我正在使用 Image.ADAPTIVE 来避免抖动

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

忆梦 2024-08-02 09:27:27

我假设您想做一些比 海报化 更复杂的事情。 正如您所说,“采样”需要一些技巧,因为图像中 5 种最常见的颜色可能彼此相似。 也许看一下 直方图中 5 个最分离的峰值

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.

不再让梦枯萎 2024-08-02 09:27:27

简短的答案是使用 Image.quantize 方法。 有关详细信息,请参阅:如何使用 Python 成像库将任何图像转换为 4 调色板图像?

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 ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文