Limit quantized palette to number of colors

This commit is contained in:
Andrew Murray 2021-12-11 16:23:37 +11:00
parent cc927343da
commit ec198899f6
2 changed files with 9 additions and 1 deletions

View File

@ -77,6 +77,13 @@ def test_quantize_dither_diff():
assert dither.tobytes() != nodither.tobytes()
def test_colors():
im = hopper()
colors = 2
converted = im.quantize(colors)
assert len(converted.palette.palette) == colors * len("RGB")
def test_transparent_colors_equal():
im = Image.new("RGBA", (1, 2), (0, 0, 0, 0))
px = im.load()

View File

@ -1111,7 +1111,8 @@ class Image:
from . import ImagePalette
mode = im.im.getpalettemode()
im.palette = ImagePalette.ImagePalette(mode, im.im.getpalette(mode, mode))
palette = im.im.getpalette(mode, mode)[: colors * len(mode)]
im.palette = ImagePalette.ImagePalette(mode, palette)
return im