简体   繁体   English

如何使用 PIL python 将 colors 的数量减少到 1024

[英]How to reduce number of colors to 1024 with PIL python

I am trying to reduce number of colors to 1024, but got error ValueError: bad number of colors我正在尝试将 colors 的数量减少到 1024,但出现错误 ValueError: bad number of colors

img = img.convert('P', palette=Image.ADAPTIVE, colors=1024)

or或者

img = img.quantize(256, 0)

What is wrong?怎么了?

As you can see here from the PIL.Image module source code https://pillow.readthedocs.io/en/stable/_modules/PIL/Image.html the documenting comment for the convert method states that:正如您从PIL.Image模块源代码https://pillow.readthedocs.io/en/stable/_modules/PIL/Image.html中看到的, convert方法的文档注释指出:

 When converting from "RGBA" to "P" without a ``matrix`` argument,
 this passes the operation to :py:meth:`~PIL.Image.Image.quantize`,
 and ``dither`` and ``palette`` are ignored.

so the quantize method is used for the operation because you didn't provide a matrix argument to the convert method and now inside of the quantize method the documenting states that:所以quantize方法用于操作,因为您没有为 convert 方法提供matrix参数,现在在quantize方法内部,文档说明:

Convert the image to 'P' mode with the specified number
of colors.

:param colors: The desired number of colors, <= 256

so the number of colors should be less than or equal to 256 instead a ValueError exception will be raised.因此 colors 的数量应小于或等于 256,而不是会引发ValueError异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM