Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AnimGIF not transparent #5656

Closed
doublex opened this issue Aug 5, 2021 · 3 comments · Fixed by #5756
Closed

AnimGIF not transparent #5656

doublex opened this issue Aug 5, 2021 · 3 comments · Fixed by #5756
Labels

Comments

@doublex
Copy link

doublex commented Aug 5, 2021

The frames of the down below animated GIF are not transparent (first frame works).

Testcase:

import base64, io
import PIL.Image

animgif = '''
R0lGODlhJgAgAPdEAAIBAQsKChkGBCUdBy4lByoqKUw5AFlFAltJE2dNAkVFRU1OTlZVVWFiYoIgAIRjAJJxApx1AaJ6AK2MJMGRAM+bANKdANagANmjAOKqBOewC+a5L//MM/7XNoqKipqamvPz8/b29ru7uiMiIL29vdHR0dnZ2f/RKjY2NUQzAHhhGWpqaqd9APO+GPnGLf///4SEhCEfGHpcAHJzc9M0ALKGAL2OAO+6Gf/ZOOzr63NWAW9xcqYmANqnDt2rE8qjKd6zLf/YNr/DxOfo6BYSBOHh4aJ9CM7Ozv/cN8ecGn1gA7KJCn5+gtzc3BoZFTELAD49PEkOAFETAmxTAG5ubtk2AP/RNZaWlqSkpOTk4/r6+3h4eLYnANbW1qSEIZGRkcfHx+q0Eubm5jcqAUU2C8+cBP/XMdbX2GdTFsHBwTAuJkE6JbGwsNzb2nBsY/TBKcQwAM00ALaKAcqXAOGuFfbDI//ONJ2dnTAsGz05LUA7LUhDOf/PNtTU0+OwF+q5HxEUFjoZARwmKS0xMmkNAHsPAGgTAGxjSZcbAOM1AO8zAP84AP/ROK2hfp+hptXPveq8MMiWAM2ZAOi1HNClI/39/f81APHx8bCKHD0wAv/SMf3GIb6TDjwwDoVqG+azGv/cMP/OJfbILHRaAZN2IKysrCEGAFA9AXYaBIcZANWiBlYJAIBnHIpvHo5yH6uxs2BOFFxcW6qCAko7D0g3AmZnbPbIMoxtFMCW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Ad7KLCAbIMYAsxEaDwwQw7ALDAKwEgYgkNRwoACxIdTEBEDRAQwIEGo8QiwgshlJLFC0IAUogiRjpAgC0uGBHBBTbMmAARJITwQikKYPHCC2cIQkgiRgqgxAXTVeBUCgAMtsULDACAwqIvDDGIIVW0CIAsN2TgWwYPjCDcFnesLQAAFB9ccYUjMwAghQMOBKnCMLoocQARpTxExbHIJovsCgo0wMACtcBQAkQBAQA7'''

with PIL.Image.open(io.BytesIO(base64.b64decode(animgif)), 'r') as img:
    for index, frame in enumerate(PIL.ImageSequence.Iterator(img)):
        img.save('/tmp/not-transparent-'+str(index)+'.png', 'PNG')
@radarhere
Copy link
Member

radarhere commented Oct 11, 2021

Experimenting, I find that prioritising the global transparency over frame transparency when replacing with background color fixes this.

diff --git a/src/PIL/GifImagePlugin.py b/src/PIL/GifImagePlugin.py
index db6944267..128afc428 100644
--- a/src/PIL/GifImagePlugin.py
+++ b/src/PIL/GifImagePlugin.py
@@ -271,11 +271,9 @@ class GifImageFile(ImageFile.ImageFile):
                 Image._decompression_bomb_check(dispose_size)
 
                 # by convention, attempt to use transparency first
-                color = (
-                    frame_transparency
-                    if frame_transparency is not None
-                    else self.info.get("background", 0)
-                )
+                color = self.info.get("transparency", frame_transparency)
+                if color is None:
+                    color = self.info.get("background", 0)
                 self.dispose = Image.core.fill("P", dispose_size, color)
             else:
                 # replace with previous contents

Is the image data that you've provided able to be included as part of our test suite, and distributed under our Pillow license?

@doublex
Copy link
Author

doublex commented Oct 11, 2021

@radarhere
Thanks a lot for all your efforts.
Yes, the GIF image is license free (created in house).

@radarhere
Copy link
Member

I've created PR #5756 to resolve this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants