Skip to content

Fix saving P images with bytes transparency as GIF - #10719

Open
mh-masum6875 wants to merge 1 commit into
python-pillow:mainfrom
mh-masum6875:fix/gif-save-bytes-transparency
Open

mh-masum6875 wants to merge 1 commit into
python-pillow:mainfrom
mh-masum6875:fix/gif-save-bytes-transparency

Conversation

@mh-masum6875

Copy link
Copy Markdown

Changes proposed in this pull request:

  • A P mode PNG with alpha values for each palette entry is opened with info["transparency"] as bytes. Saving that image as GIF raised a TypeError, since GIF expects a single index.
  • When saving, I now use the first fully transparent entry as the GIF transparency index. If there isn't one, the image is saved without transparency.
  • The same is done for the first frame's transparency when a frame follows one with disposal method 2.
from PIL import Image

im = Image.new("P", (4, 1))
im.putpalette((255, 0, 0, 0, 255, 0, 0, 0, 255, 255, 255, 255))
im.putdata([0, 1, 2, 3])
im.save("in.png", transparency=b"\xff\x00\x80\x00")

with Image.open("in.png") as im:
    im.save("out.gif")
# TypeError: unsupported operand type(s) for &: 'bytes' and 'int'

I only ran this against the 13.0.0 wheel with the change applied on top, as I don't have a C toolchain set up here.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant