From 4e127ecd8f6ef23ef9b39b11d443f13c7e936836 Mon Sep 17 00:00:00 2001 From: Maidul Hasan Masum Date: Thu, 3 Dec 2026 21:43:00 +0800 Subject: [PATCH 1/2] Update transparency when quantizing --- Tests/test_image_quantize.py | 47 ++++++++++++++++++++++++++++++++++++ src/PIL/Image.py | 27 +++++++++++++++++++++ 2 files changed, 74 insertions(+) diff --git a/Tests/test_image_quantize.py b/Tests/test_image_quantize.py index ec1c13977d1..ff09e68c40a 100644 --- a/Tests/test_image_quantize.py +++ b/Tests/test_image_quantize.py @@ -190,3 +190,50 @@ def test_empty_palette() -> None: with pytest.raises(ValueError, match="empty palette"): im.quantize(palette=p) + + +@pytest.mark.parametrize( + "mode, color, transparency", + ( + ("RGB", (255, 0, 0), (255, 0, 0)), + ("L", 200, 200), + ), +) +def test_transparency( + mode: str, color: int | tuple[int, ...], transparency: int | tuple[int, ...] +) -> None: + im = Image.new(mode, (2, 1)) + im.putpixel((0, 0), color) + im.info["transparency"] = transparency + + converted = im.quantize() + assert converted.info["transparency"] == converted.getpixel((0, 0)) + assert converted.info["transparency"] != converted.getpixel((1, 0)) + + +def test_transparency_p() -> None: + im = Image.new("P", (2, 1)) + im.putpalette((0, 0, 0) * 5 + (255, 0, 0)) + im.putpixel((0, 0), 5) + im.info["transparency"] = 5 + + converted = im.quantize() + assert converted.info["transparency"] == converted.getpixel((0, 0)) + assert converted.info["transparency"] != converted.getpixel((1, 0)) + + +def test_transparency_palette() -> None: + im = Image.new("RGB", (2, 1)) + im.putpixel((0, 0), (255, 0, 0)) + im.info["transparency"] = (255, 0, 0) + + p = Image.new("P", (1, 1)) + p.putpalette((0, 0, 0, 255, 0, 0)) + + converted = im.quantize(palette=p) + assert converted.info["transparency"] == 1 + + # If the transparent color is not in the palette + im.info["transparency"] = (0, 255, 0) + converted = im.quantize(palette=p) + assert "transparency" not in converted.info diff --git a/src/PIL/Image.py b/src/PIL/Image.py index a54bd282d6c..cc702e505ff 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -1362,6 +1362,10 @@ def quantize( new_im = self._new(im) assert palette.palette is not None new_im.palette = palette.palette.copy() + if self.mode != "L": + # L pixel values are used as the palette indexes, + # so the transparency is still correct + self._quantize_transparency(new_im) return new_im if kmeans < 0: @@ -1375,9 +1379,32 @@ def quantize( mode = im.im.getpalettemode() palette_data = im.im.getpalette()[: colors * len(mode)] im.palette = ImagePalette.ImagePalette(mode, palette_data) + self._quantize_transparency(im) return im + def _quantize_transparency(self, im: Image) -> None: + # The transparency of this image does not apply to the new palette + t = im.info.pop("transparency", None) + if t is None or isinstance(t, bytes): + return + + # Find the transparent color in the new palette + trns: tuple[int, ...] + if isinstance(t, tuple): + trns = t + elif self.mode == "P": + palette = self.getpalette() + assert palette is not None + trns = tuple(palette[t * 3 : t * 3 + 3]) + else: + trns = (t, t, t) + + assert im.palette is not None + index = im.palette.colors.get(trns) + if index is not None: + im.info["transparency"] = index + def copy(self) -> Image: """ Copies this image. Use this method if you wish to paste things From c7cb2772254cd0b7dfbf2d3e687e2cec7f2c8697 Mon Sep 17 00:00:00 2001 From: Maidul Hasan Masum Date: Thu, 3 Dec 2026 23:33:20 +0800 Subject: [PATCH 2/2] Keep the alpha of each palette entry when quantizing a P image --- Tests/test_image_quantize.py | 28 ++++++++++++++++++++++++++++ src/PIL/Image.py | 20 ++++++++++++++++++-- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/Tests/test_image_quantize.py b/Tests/test_image_quantize.py index ff09e68c40a..3c720a810ca 100644 --- a/Tests/test_image_quantize.py +++ b/Tests/test_image_quantize.py @@ -5,6 +5,7 @@ from PIL import Image from .helper import ( + assert_image_equal, assert_image_similar, has_feature_version, hopper, @@ -237,3 +238,30 @@ def test_transparency_palette() -> None: im.info["transparency"] = (0, 255, 0) converted = im.quantize(palette=p) assert "transparency" not in converted.info + + +@pytest.mark.parametrize( + "palette, transparency", + ( + ((255, 255, 255, 128, 128, 128, 0, 0, 0), b"\x00\x80\xff"), + ((0, 0, 0, 0, 0, 255, 255, 0, 0, 0, 255, 0), b"\xff\x00\x80"), + ), +) +def test_transparency_p_bytes(palette: tuple[int, ...], transparency: bytes) -> None: + im = Image.new("P", (len(palette) // 3, 1)) + im.putpalette(palette) + im.putdata(range(im.width)) + im.info["transparency"] = transparency + + converted = im.quantize(im.width) + assert_image_equal(converted.convert("RGBA"), im.convert("RGBA")) + + +def test_transparency_p_bytes_opaque() -> None: + im = Image.new("P", (2, 1)) + im.putpalette((0, 0, 0, 255, 0, 0)) + im.putpixel((1, 0), 1) + im.info["transparency"] = b"\xff\xff" + + converted = im.quantize() + assert "transparency" not in converted.info diff --git a/src/PIL/Image.py b/src/PIL/Image.py index cc702e505ff..83a8eda2fdb 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -1386,7 +1386,24 @@ def quantize( def _quantize_transparency(self, im: Image) -> None: # The transparency of this image does not apply to the new palette t = im.info.pop("transparency", None) - if t is None or isinstance(t, bytes): + if t is None: + return + assert im.palette is not None + + if isinstance(t, bytes): + # Alpha values for each entry of the old palette. + # Give each color the same alpha in the new palette + if self.mode != "P": + return + palette = self.getpalette() + assert palette is not None + alpha = bytearray(b"\xff" * len(im.palette.colors)) + for i in reversed(range(min(len(t), len(palette) // 3))): + index = im.palette.colors.get(tuple(palette[i * 3 : i * 3 + 3])) + if index is not None: + alpha[index] = t[i] + if alpha.count(255) != len(alpha): + im.info["transparency"] = bytes(alpha) return # Find the transparent color in the new palette @@ -1400,7 +1417,6 @@ def _quantize_transparency(self, im: Image) -> None: else: trns = (t, t, t) - assert im.palette is not None index = im.palette.colors.get(trns) if index is not None: im.info["transparency"] = index