diff --git a/Tests/test_image_quantize.py b/Tests/test_image_quantize.py index 9f56575a8..53b6c9007 100644 --- a/Tests/test_image_quantize.py +++ b/Tests/test_image_quantize.py @@ -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() diff --git a/src/PIL/Image.py b/src/PIL/Image.py index 3631bd869..d5113a71f 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -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