Merge pull request #5879 from radarhere/quantize

Limit quantized palette to number of colors
This commit is contained in:
Hugo van Kemenade 2021-12-28 10:08:54 +02:00 committed by GitHub
commit 17ec8b5c98
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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

@ -1109,7 +1109,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