Skip to content

GIF: avoid writing duplicate global header for transparent frame after disposal 2 - #10692

Open
ekanshbannsal wants to merge 2 commits into
python-pillow:mainfrom
ekanshbannsal:fix/gif-dispose2-transparent-frame
Open

ekanshbannsal wants to merge 2 commits into
python-pillow:mainfrom
ekanshbannsal:fix/gif-dispose2-transparent-frame

Conversation

@ekanshbannsal

Copy link
Copy Markdown

Fixes #10691 .

Changes proposed in this pull request:

  • In GifImagePlugin._write_multiple_frames, fall back to a 1x1 (0, 0, 1, 1) bounding box when _getbbox(background_im, im_frame)[1] returns None after a frame with disposal=2 and transparency, preventing the subsequent empty/fully-transparent frame from being treated as frame 0 (if not frame_data.bbox:) and emitting a duplicate GIF89a global header and color table.
  • Add a regression test test_dispose2_transparent_frame_no_duplicate_header verifying a single GIF89a header, all 3 frames preserved, and the expected RGBA contents across every frame.

Root Cause

When a frame follows a disposal=2 frame with transparency configured, _write_multiple_frames computes its bounding box against background_im. If the subsequent frame is completely transparent, _getbbox(background_im, im_frame)[1] returns None, which caused the write loop's if not frame_data.bbox: check to write a second global header mid-stream.

Testing

  • pytest Tests/test_file_gif.py Tests/test_imagesequence.py: 126 passed, 2 skipped (unrelated: littlecms2 and libtiff not installed in local environment).
  • ruff check src/PIL/GifImagePlugin.py Tests/test_file_gif.py: All checks passed.

@radarhere radarhere added the GIF label Dec 2, 2026

@gastondana627 gastondana627 left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Reviewed the diff and GifImagePlugin._write_multiple_frames:

  • Fix & test look solid: Falling back to (0, 0, 1, 1) when _getbbox(background_im, im_frame)[1] is None directly fixes #10691, and test_dispose2_transparent_frame_no_duplicate_header covers the single GIF89a header, frame count, and expected RGBA bboxes across all frames.
  • Interaction with #10617: Note that open PR #10617 also adds if not bbox: bbox = (0, 0, 1, 1) right below the previous_disposal == 2 / 3 branches (to handle both disposal 2 and 3), though it does not have this disposal=2 transparent-frame regression test — either way this test is great to keep.
  • Two small non-blocking thoughts:
    1. A quick check with two consecutive fully-transparent frames after disposal=2 (to verify frame deduplication + combined duration alongside the single header) might be nice coverage.
    2. Longer term, the write loop currently uses if not frame_data.bbox: as the sentinel for frame 0 instead of checking i == 0 via enumerate(im_frames), which is how any non-first None bbox accidentally re-emits the global header.

@ekanshbannsal
ekanshbannsal force-pushed the fix/gif-dispose2-transparent-frame branch from 0c5e346 to f703fda Compare December 2, 2026 18:02
@ekanshbannsal

Copy link
Copy Markdown
Author

Addressed both non-blocking suggestions from the review in f703fda:

  1. Consecutive transparent frames coverage — added test_dispose2_consecutive_transparent_frames: two fully-transparent frames after a disposal=2 frame. It asserts a single GIF89a header, that the second transparent frame is deduplicated (2 frames), and that its duration is combined (200 ms).
  2. Header sentinel — _write_multiple_frames now iterates with enumerate(im_frames) and writes the global header only for i == 0. A non-first frame with a None bbox falls back to the full-frame bbox instead of re-emitting the header.

I also fixed the red Lint job: mypy flagged im.n_frames on ImageFile in the new test, now narrowed with assert isinstance(im, GifImagePlugin.GifImageFile) (same pattern as the neighbouring disposal tests).

Verified locally:

  • Tests/test_file_gif.py: 120 passed, 1 skipped.
  • Both disposal-2 tests fail when run against the pre-fix GifImagePlugin.py, and pass with the fix.
  • mypy clean on both changed files; ruff check and black --check clean.

The Python 3.14t (x64) failure is unrelated to this change — it fails while building the wheel on Windows free-threaded (PyQt6 has no 3.14t distribution and the build backend asserts "don't init multiple times") before any tests run.

Re #10617: no conflict — that PR adds a (0, 0, 1, 1) fallback for disposal 3 as well, while this PR keeps the regression test for disposal 2; whichever lands second can retain both.

@radarhere radarhere added the 🤖-assisted AI-assisted label Dec 3, 2026
@ekanshbannsal

Copy link
Copy Markdown
Author

Quick update on f703fda: both suggestions from the review are in (consecutive-transparent-frames coverage and the enumerate/i == 0 header sentinel), and all 56 checks now pass — including Lint and Python 3.14t (x64), the latter having been a transient Windows free-threaded wheel-build failure before any tests ran. Nothing else is pending from the review.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Saving a GIF with a fully transparent frame after a disposal 2 frame writes a second file header

3 participants