From 0dba28613bc94084dc668b924db7e6b5ec944a87 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Tue, 31 Aug 2021 00:33:10 +1000 Subject: [PATCH] Copy Python palette to new image in quantize() --- Tests/test_image_quantize.py | 1 + src/PIL/Image.py | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/Tests/test_image_quantize.py b/Tests/test_image_quantize.py index 1ceff0842..bd9db362c 100644 --- a/Tests/test_image_quantize.py +++ b/Tests/test_image_quantize.py @@ -63,6 +63,7 @@ def test_quantize_no_dither(): converted = image.quantize(dither=0, palette=palette) assert_image(converted, "P", converted.size) + assert converted.palette.palette == palette.palette.palette def test_quantize_dither_diff(): diff --git a/src/PIL/Image.py b/src/PIL/Image.py index f53dbe016..7dd5b35bd 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -1130,7 +1130,9 @@ class Image: "only RGB or L mode images can be quantized to a palette" ) im = self.im.convert("P", dither, palette.im) - return self._new(im) + new_im = self._new(im) + new_im.palette = palette.palette.copy() + return new_im im = self._new(self.im.quantize(colors, method, kmeans))