From 6d974b61d6144fab9e65aff1b84bedd377737db8 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Fri, 15 Aug 2025 14:37:31 +1000 Subject: [PATCH] Load image palette into Python after converting to PA --- Tests/test_image_convert.py | 8 ++++++++ src/PIL/Image.py | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/Tests/test_image_convert.py b/Tests/test_image_convert.py index 33f844437..7ba3fb555 100644 --- a/Tests/test_image_convert.py +++ b/Tests/test_image_convert.py @@ -107,6 +107,14 @@ def test_rgba_p() -> None: assert_image_similar(im, comparable, 20) +def test_pa() -> None: + im = hopper().convert("PA") + + palette = im.palette + assert palette is not None + assert palette.colors != {} + + def test_rgba() -> None: with Image.open("Tests/images/transparent.png") as im: assert im.mode == "RGBA" diff --git a/src/PIL/Image.py b/src/PIL/Image.py index b7c185e0d..b435b17ec 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -1143,7 +1143,7 @@ class Image: raise ValueError(msg) from e new_im = self._new(im) - if mode == "P" and palette != Palette.ADAPTIVE: + if mode in ("P", "PA") and palette != Palette.ADAPTIVE: from . import ImagePalette new_im.palette = ImagePalette.ImagePalette("RGB", im.getpalette("RGB"))