I am trying to reduce number of colors to 1024, but got error ValueError: bad number of colors
img = img.convert('P', palette=Image.ADAPTIVE, colors=1024)
or
img = img.quantize(256, 0)
What is wrong?
I am trying to reduce number of colors to 1024, but got error 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:
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:
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.