From ba66fec3d242fc1a8b287ba00baaed766b60786a Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Fri, 15 Aug 2025 23:39:33 +1000 Subject: [PATCH] When converting RGBA to PA, use RGB to P quantization --- Tests/test_image_convert.py | 7 +++++++ src/PIL/Image.py | 10 ++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/Tests/test_image_convert.py b/Tests/test_image_convert.py index 33f844437..6c7026d47 100644 --- a/Tests/test_image_convert.py +++ b/Tests/test_image_convert.py @@ -107,6 +107,13 @@ def test_rgba_p() -> None: assert_image_similar(im, comparable, 20) +def test_rgba_pa() -> None: + im = hopper("RGBA").convert("PA").convert("RGB") + expected = hopper("RGB") + + assert_image_similar(im, expected, 9.3) + + 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..55309adbc 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -1010,8 +1010,14 @@ class Image: new_im.info["transparency"] = transparency return new_im - if mode == "P" and self.mode == "RGBA": - return self.quantize(colors) + if self.mode == "RGBA": + if mode == "P": + return self.quantize(colors) + elif mode == "PA": + r, g, b, a = self.split() + rgb = merge("RGB", (r, g, b)) + p = rgb.quantize(colors) + return merge("PA", (p, a)) trns = None delete_trns = False