When converting RGBA to PA, use RGB to P quantization

This commit is contained in:
Andrew Murray 2025-08-15 23:39:33 +10:00
parent 0ae2611b44
commit ba66fec3d2
2 changed files with 15 additions and 2 deletions

View File

@ -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"

View File

@ -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