diff --git a/Tests/test_image_convert.py b/Tests/test_image_convert.py index a4c5490102f..7a8a9e78c89 100644 --- a/Tests/test_image_convert.py +++ b/Tests/test_image_convert.py @@ -317,6 +317,20 @@ def test_p2pa_palette() -> None: assert im_pa.getpalette(None) == im.getpalette() +@pytest.mark.parametrize( + "mode, value", + (("1", 255), ("L", 128), ("RGB", (1, 2, 3)), ("I;16", 0x1234), ("I", 0x12345)), +) +def test_trns_to_pa(mode: str, value: int | tuple[int, int, int]) -> None: + im = Image.new(mode, (2, 1)) + im.putpixel((1, 0), value) + im.info["transparency"] = value + + im_pa = im.convert("PA") + assert "transparency" not in im_pa.info + assert im_pa.getchannel("A").get_flattened_data() == (255, 0) + + rgb2xyz_matrix = ( 0.412453, 0.357580, 0.180423, 0, 0.212671, 0.715160, 0.072169, 0, diff --git a/src/PIL/Image.py b/src/PIL/Image.py index c76bebcfb94..8c837a5f661 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -1198,6 +1198,12 @@ def convert_transparency( else: msg = "Transparency for P mode should be bytes or int" raise ValueError(msg) + elif self.mode in {"1", "L", "I", "I;16", "RGB"} and mode == "PA": + im_without_trns = self.copy() + del im_without_trns.info["transparency"] + new_im = im_without_trns.convert(mode, dither=dither) + new_im.putalpha(self.convert("RGBA").getchannel("A")) + return new_im if mode == "P" and palette == Palette.ADAPTIVE: im = self.im.quantize(colors)