Pillow icon indicating copy to clipboard operation
Pillow copied to clipboard

When saving GIF frame that restores to background color, do not fill identical pixels

Open radarhere opened this issue 1 year ago • 1 comments

Resolves #7787

#7568 set identical pixels in subsequent GIF frames to be transparency.

This new issue has found that for disposal 2, when GIF disposes the previous frame and restores it to the background color, filling the identical pixels with transparency results in those pixels being seen as transparent by viewers.

This PR skips the optimisation step of filling identical pixels when the previous frame is restored to the background colour, as it is unnecessary when the frame is being compared with a single color (the background). The filling of pixels that match the previous frame is only an optimisation when it is reducing multiple possible matching colors to one color.

radarhere avatar Feb 09 '24 08:02 radarhere

In my first commit here, I used the previous frame's disposal when considered whether the current frame should be compared to the background color or not.

However, the current frame's disposal was being used when generating background color frame for comparison. https://github.com/python-pillow/Pillow/blob/6782a07b8e404271dbd2e5ddf5fbb93e575ed2bc/src/PIL/GifImagePlugin.py#L632-L640

This is incorrect, and so I've pushed another commit with a new test. Disposal refers to how the current frame will be treated when you have moved on to the next one - how the current frame is disposed of.

radarhere avatar Feb 10 '24 10:02 radarhere