Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Tests/test_image_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 6 additions & 0 deletions src/PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading