Only crop when saving with disposal method 2 if transparency is present

This commit is contained in:
Andrew Murray 2025-02-12 19:12:27 +11:00
parent 3c2310e090
commit 8f4bfe1fe5
2 changed files with 32 additions and 11 deletions

View File

@ -762,6 +762,21 @@ def test_dispose2_previous_frame(tmp_path: Path) -> None:
assert im.getpixel((0, 0)) == (0, 0, 0, 255) assert im.getpixel((0, 0)) == (0, 0, 0, 255)
def test_dispose2_without_transparency(tmp_path: Path) -> None:
out = str(tmp_path / "temp.gif")
im = Image.new("P", (100, 100))
im2 = Image.new("P", (100, 100), (0, 0, 0))
im2.putpixel((50, 50), (255, 0, 0))
im.save(out, save_all=True, append_images=[im2], disposal=2)
with Image.open(out) as reloaded:
reloaded.seek(1)
assert reloaded.tile[0].extents == (0, 0, 100, 100)
def test_transparency_in_second_frame(tmp_path: Path) -> None: def test_transparency_in_second_frame(tmp_path: Path) -> None:
out = str(tmp_path / "temp.gif") out = str(tmp_path / "temp.gif")
with Image.open("Tests/images/different_transparency.gif") as im: with Image.open("Tests/images/different_transparency.gif") as im:

View File

@ -689,16 +689,21 @@ def _write_multiple_frames(
im_frames[-1].encoderinfo["duration"] += encoderinfo["duration"] im_frames[-1].encoderinfo["duration"] += encoderinfo["duration"]
continue continue
if im_frames[-1].encoderinfo.get("disposal") == 2: if im_frames[-1].encoderinfo.get("disposal") == 2:
if background_im is None: # To appear correctly in viewers using a convention,
color = im.encoderinfo.get( # only consider transparency, and not background color
"transparency", im.info.get("transparency", (0, 0, 0)) color = im.encoderinfo.get(
) "transparency", im.info.get("transparency")
background = _get_background(im_frame, color) )
background_im = Image.new("P", im_frame.size, background) if color is not None:
first_palette = im_frames[0].im.palette if background_im is None:
assert first_palette is not None background = _get_background(im_frame, color)
background_im.putpalette(first_palette, first_palette.mode) background_im = Image.new("P", im_frame.size, background)
bbox = _getbbox(background_im, im_frame)[1] first_palette = im_frames[0].im.palette
assert first_palette is not None
background_im.putpalette(first_palette, first_palette.mode)
bbox = _getbbox(background_im, im_frame)[1]
else:
bbox = (0, 0) + im_frame.size
elif encoderinfo.get("optimize") and im_frame.mode != "1": elif encoderinfo.get("optimize") and im_frame.mode != "1":
if "transparency" not in encoderinfo: if "transparency" not in encoderinfo:
assert im_frame.palette is not None assert im_frame.palette is not None
@ -764,7 +769,8 @@ def _write_multiple_frames(
if not palette: if not palette:
frame_data.encoderinfo["include_color_table"] = True frame_data.encoderinfo["include_color_table"] = True
im_frame = im_frame.crop(frame_data.bbox) if frame_data.bbox != (0, 0) + im_frame.size:
im_frame = im_frame.crop(frame_data.bbox)
offset = frame_data.bbox[:2] offset = frame_data.bbox[:2]
_write_frame_data(fp, im_frame, offset, frame_data.encoderinfo) _write_frame_data(fp, im_frame, offset, frame_data.encoderinfo)
return True return True