Consider transparency from each frame when saving

This commit is contained in:
Andrew Murray 2022-04-02 21:04:22 +11:00
parent a575ec33d2
commit 6b2da2f123
2 changed files with 15 additions and 2 deletions

View File

@ -619,7 +619,7 @@ def test_dispose2_background(tmp_path):
assert im.getpixel((0, 0)) == (255, 0, 0)
def test_transparency_in_second_frame():
def test_transparency_in_second_frame(tmp_path):
with Image.open("Tests/images/different_transparency.gif") as im:
assert im.info["transparency"] == 0
@ -629,6 +629,15 @@ def test_transparency_in_second_frame():
assert_image_equal_tofile(im, "Tests/images/different_transparency_merged.png")
out = str(tmp_path / "temp.gif")
im.save(out, save_all=True)
with Image.open(out) as reread:
reread.seek(reread.tell() + 1)
assert_image_equal_tofile(
reread, "Tests/images/different_transparency_merged.png"
)
def test_no_transparency_in_second_frame():
with Image.open("Tests/images/iss634.gif") as img:

View File

@ -573,10 +573,14 @@ def _write_multiple_frames(im, fp, palette):
im_frame = _normalize_mode(im_frame.copy())
if frame_count == 0:
for k, v in im_frame.info.items():
if k == "transparency":
continue
im.encoderinfo.setdefault(k, v)
im_frame = _normalize_palette(im_frame, palette, im.encoderinfo)
encoderinfo = im.encoderinfo.copy()
if "transparency" in im_frame.info:
encoderinfo.setdefault("transparency", im_frame.info["transparency"])
im_frame = _normalize_palette(im_frame, palette, encoderinfo)
if isinstance(duration, (list, tuple)):
encoderinfo["duration"] = duration[frame_count]
if isinstance(disposal, (list, tuple)):