Skip to content

Commit 93bf991

Browse files
authored
Merge pull request #7788 from radarhere/gif_disposal
2 parents 350d7f2 + 430f506 commit 93bf991

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

Tests/test_file_gif.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,9 @@ def test_dispose2_palette(tmp_path: Path) -> None:
647647
# Center remains red every frame
648648
assert rgb_img.getpixel((50, 50)) == circle
649649

650+
# Check that frame transparency wasn't added unnecessarily
651+
assert img._frame_transparency is None
652+
650653

651654
def test_dispose2_diff(tmp_path: Path) -> None:
652655
out = str(tmp_path / "temp.gif")
@@ -734,6 +737,25 @@ def test_dispose2_background_frame(tmp_path: Path) -> None:
734737
assert im.n_frames == 3
735738

736739

740+
def test_dispose2_previous_frame(tmp_path: Path) -> None:
741+
out = str(tmp_path / "temp.gif")
742+
743+
im = Image.new("P", (100, 100))
744+
im.info["transparency"] = 0
745+
d = ImageDraw.Draw(im)
746+
d.rectangle([(0, 0), (100, 50)], 1)
747+
im.putpalette((0, 0, 0, 255, 0, 0))
748+
749+
im2 = Image.new("P", (100, 100))
750+
im2.putpalette((0, 0, 0))
751+
752+
im.save(out, save_all=True, append_images=[im2], disposal=[0, 2])
753+
754+
with Image.open(out) as im:
755+
im.seek(1)
756+
assert im.getpixel((0, 0)) == (0, 0, 0, 255)
757+
758+
737759
def test_transparency_in_second_frame(tmp_path: Path) -> None:
738760
out = str(tmp_path / "temp.gif")
739761
with Image.open("Tests/images/different_transparency.gif") as im:

src/PIL/GifImagePlugin.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -629,16 +629,16 @@ def _write_multiple_frames(im, fp, palette):
629629
"duration"
630630
]
631631
continue
632-
if encoderinfo.get("disposal") == 2:
632+
if im_frames[-1]["encoderinfo"].get("disposal") == 2:
633633
if background_im is None:
634634
color = im.encoderinfo.get(
635635
"transparency", im.info.get("transparency", (0, 0, 0))
636636
)
637637
background = _get_background(im_frame, color)
638638
background_im = Image.new("P", im_frame.size, background)
639639
background_im.putpalette(im_frames[0]["im"].palette)
640-
delta, bbox = _getbbox(background_im, im_frame)
641-
if encoderinfo.get("optimize") and im_frame.mode != "1":
640+
bbox = _getbbox(background_im, im_frame)[1]
641+
elif encoderinfo.get("optimize") and im_frame.mode != "1":
642642
if "transparency" not in encoderinfo:
643643
try:
644644
encoderinfo["transparency"] = (

0 commit comments

Comments
 (0)