Skip to content

Commit ba66fec

Browse files
committed
When converting RGBA to PA, use RGB to P quantization
1 parent 0ae2611 commit ba66fec

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

Tests/test_image_convert.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,13 @@ def test_rgba_p() -> None:
107107
assert_image_similar(im, comparable, 20)
108108

109109

110+
def test_rgba_pa() -> None:
111+
im = hopper("RGBA").convert("PA").convert("RGB")
112+
expected = hopper("RGB")
113+
114+
assert_image_similar(im, expected, 9.3)
115+
116+
110117
def test_rgba() -> None:
111118
with Image.open("Tests/images/transparent.png") as im:
112119
assert im.mode == "RGBA"

src/PIL/Image.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,8 +1010,14 @@ def convert_transparency(
10101010
new_im.info["transparency"] = transparency
10111011
return new_im
10121012

1013-
if mode == "P" and self.mode == "RGBA":
1014-
return self.quantize(colors)
1013+
if self.mode == "RGBA":
1014+
if mode == "P":
1015+
return self.quantize(colors)
1016+
elif mode == "PA":
1017+
r, g, b, a = self.split()
1018+
rgb = merge("RGB", (r, g, b))
1019+
p = rgb.quantize(colors)
1020+
return merge("PA", (p, a))
10151021

10161022
trns = None
10171023
delete_trns = False

0 commit comments

Comments
 (0)