Skip to content
Open
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
12 changes: 12 additions & 0 deletions Tests/test_image_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,18 @@ def test_p2pa_palette() -> None:
assert im_pa.getpalette() == im.getpalette()


def test_pa2p_truly_drops_alpha() -> None:
im = Image.frombytes("P", (2, 1), bytes([0, 1])).convert("PA")
im.putpalette(bytes([255, 0, 0, 7, 0, 255, 0, 9]), "RGBA")
im.putalpha(Image.frombytes("L", (2, 1), bytes([240, 220])))
assert im.get_flattened_data() == ((0, 240), (1, 220)) # Matches the alpha band
Comment on lines +296 to +297

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
im.putalpha(Image.frombytes("L", (2, 1), bytes([240, 220])))
assert im.get_flattened_data() == ((0, 240), (1, 220)) # Matches the alpha band

These lines don't appear relevant.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They're meant to show the palette alpha isn't used.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I consider it pretty foundational that the palette doesn't affect pixel values (without conversion). I'm not sure it needs to be tested here? We already have

im = Image.new("P", (1, 1), 1)
assert im.getpixel((0, 0)) == 1
im.putalpha(2)
assert im.mode == "PA"
assert im.getpixel((0, 0)) == (1, 2)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That test_image_putalpha segment doesn't seem to really cover the contents of the palette.

I don't see how having this here really hurts?

im_p = im.convert("P")
assert im_p.palette is not None
assert im_p.im.getpalettemode() == im_p.palette.mode == "RGB"
rgba_data = im_p.convert("RGBA").get_flattened_data()
assert rgba_data == ((255, 0, 0, 255), (0, 255, 0, 255))


def test_matrix_illegal_conversion() -> None:
# Arrange
im = hopper("CMYK")
Expand Down
5 changes: 2 additions & 3 deletions src/PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
Loading