From 541d2605b9235a427379e3df51c9cdd4ffe59998 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Wed, 14 Jun 2023 14:21:07 +1000 Subject: [PATCH] Allow alpha differences to indicate different frames when saving GIF --- Tests/test_file_gif.py | 12 ++++++++++++ src/PIL/GifImagePlugin.py | 4 ++-- src/PIL/PngImagePlugin.py | 2 +- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/Tests/test_file_gif.py b/Tests/test_file_gif.py index 0e50ee1abf9..f4a17264f4a 100644 --- a/Tests/test_file_gif.py +++ b/Tests/test_file_gif.py @@ -1130,6 +1130,18 @@ def test_bbox(tmp_path): assert reread.n_frames == 2 +def test_bbox_alpha(tmp_path): + out = str(tmp_path / "temp.gif") + + im = Image.new("RGBA", (1, 2), (255, 0, 0, 255)) + im.putpixel((0, 1), (255, 0, 0, 0)) + im2 = Image.new("RGBA", (1, 2), (255, 0, 0, 0)) + im.save(out, save_all=True, append_images=[im2]) + + with Image.open(out) as reread: + assert reread.n_frames == 2 + + def test_palette_save_L(tmp_path): # Generate an L mode image with a separate palette diff --git a/src/PIL/GifImagePlugin.py b/src/PIL/GifImagePlugin.py index 2f92e946751..cf2993e3892 100644 --- a/src/PIL/GifImagePlugin.py +++ b/src/PIL/GifImagePlugin.py @@ -569,9 +569,9 @@ def _getbbox(base_im, im_frame): delta = ImageChops.subtract_modulo(im_frame, base_im) else: delta = ImageChops.subtract_modulo( - im_frame.convert("RGB"), base_im.convert("RGB") + im_frame.convert("RGBA"), base_im.convert("RGBA") ) - return delta.getbbox() + return delta.getbbox(alpha_only=False) def _write_multiple_frames(im, fp, palette): diff --git a/src/PIL/PngImagePlugin.py b/src/PIL/PngImagePlugin.py index ea3052fbf7c..bfa8cb7ac66 100644 --- a/src/PIL/PngImagePlugin.py +++ b/src/PIL/PngImagePlugin.py @@ -1140,7 +1140,7 @@ def _write_multiple_frames(im, fp, chunk, rawmode, default_image, append_images) delta = ImageChops.subtract_modulo( im_frame.convert("RGBA"), base_im.convert("RGBA") ) - bbox = delta.im.getbbox(False) + bbox = delta.getbbox(alpha_only=False) if ( not bbox and prev_disposal == encoderinfo.get("disposal")