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

Setting disposal=2 on gif export can cause issues with transparency #7787

Closed
kumquat-ir opened this issue Feb 9, 2024 · 2 comments · Fixed by #7788
Closed

Setting disposal=2 on gif export can cause issues with transparency #7787

kumquat-ir opened this issue Feb 9, 2024 · 2 comments · Fixed by #7788
Labels

Comments

@kumquat-ir
Copy link

What did you do?

Created a sequence of images with jittering text and saved them as a gif, specifying disposal=2

What did you expect to happen?

The gif to correctly display the images, with a consistent background

What actually happened?

The background flashes between opaque and transparent, with which frames are which depending on how exactly the text is jittering
testout

What are your OS, Python and Pillow versions?

  • OS: Linux (Arch)
  • Python: 3.11.6
  • Pillow: 10.2.0

This code with these images was used to generate the above gif:

from PIL import Image

baseimage = Image.open("test0.png")
images = [
    Image.open("test1.png")
]
baseimage.save("testout.gif", save_all=True, append_images=images, loop=0, duration=1000, disposal=2)

test0.png:
test0
test1.png:
test1

As far as I can tell, this is occurring because of the optimization in #7568 depending on a delta image which, when disposal is 2, can have been generated with a background image that is filled with a color with a palette index that does not exist in the frame's palette, presumably creating a nonsense delta.
Moving these lines of code outside the if statement seems to fix the issue, presumably due to _get_background having a side effect of placing the appropriate color into the frame's palette with the same index as in the background.
This does not occur when disposal is set to any other value or when optimize is set to False due to skipping one or both of the codepaths involved in the bug.

@radarhere
Copy link
Member

I've created PR #7788 to resolve this.

@kumquat-ir
Copy link
Author

Can confirm that PR fixes it, probably the best solution since a delta with a solid color, unchanging background isnt gonna be less data than the original frame.

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