Skip to content

Commit

Permalink
Allow alpha differences to indicate different frames when saving GIF
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Jun 14, 2023
1 parent 119a0df commit 541d260
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
12 changes: 12 additions & 0 deletions Tests/test_file_gif.py
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions src/PIL/GifImagePlugin.py
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/PngImagePlugin.py
Expand Up @@ -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")
Expand Down

0 comments on commit 541d260

Please sign in to comment.