diff --git a/Tests/test_image_convert.py b/Tests/test_image_convert.py index f57d83dac1a..eee5127a845 100644 --- a/Tests/test_image_convert.py +++ b/Tests/test_image_convert.py @@ -290,6 +290,18 @@ def test_p2pa_palette() -> None: assert im_pa.getpalette() == im.getpalette() +def test_pa2p_palette() -> None: + im_pa = Image.frombytes("PA", (2, 1), bytes([0, 255, 1, 255])) + im_pa.putpalette(bytes([255, 0, 0, 7, 0, 255, 0, 9]), "RGBA") + + im_pa.putalpha(Image.frombytes("L", (2, 1), bytes([240, 220]))) + assert im_pa.get_flattened_data() == ((0, 240), (1, 220)) # Matches the alpha band + + im_p = im_pa.convert("P") + assert im_p.im.getpalettemode() == "RGB" + assert im_p.im.getpalette("RGB") == bytes([255, 0, 0, 0, 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 ddbaec20a05..1c1f5af0f01 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -1242,9 +1242,8 @@ def convert_transparency( new_im = self._new(im) if mode in ("P", "PA") and palette != Palette.ADAPTIVE: - from . import ImagePalette - - new_im.palette = ImagePalette.ImagePalette("RGB", im.getpalette("RGB")) + # Install the original image's palette into the new copy + new_im.putpalette(im.getpalette("RGB")) if delete_trns: # crash fail if we leave a bytes transparency in an rgb/l mode. del new_im.info["transparency"]