diff --git a/Tests/test_file_apng.py b/Tests/test_file_apng.py index 60849e74e..145781ccc 100644 --- a/Tests/test_file_apng.py +++ b/Tests/test_file_apng.py @@ -1,5 +1,6 @@ from __future__ import annotations +import os from io import BytesIO from pathlib import Path @@ -710,11 +711,9 @@ def test_save_all_progress() -> None: out = BytesIO() progress = [] - def callback(state): + def callback(state) -> None: if state["image_filename"]: - state["image_filename"] = ( - state["image_filename"].replace("\\", "/").split("Tests/images/")[-1] - ) + state["image_filename"] = os.path.basename(state["image_filename"]) progress.append(state) Image.new("RGB", (1, 1)).save(out, "PNG", save_all=True, progress=callback) @@ -741,7 +740,7 @@ def test_save_all_progress() -> None: expected.append( { "image_index": i, - "image_filename": "apng/single_frame.png", + "image_filename": "single_frame.png", "completed_frames": i + 1, "total_frames": 7, } @@ -750,7 +749,7 @@ def test_save_all_progress() -> None: expected.append( { "image_index": 2, - "image_filename": "apng/delay.png", + "image_filename": "delay.png", "completed_frames": i + 3, "total_frames": 7, } diff --git a/Tests/test_file_avif.py b/Tests/test_file_avif.py index bd87947c0..e83e033e3 100644 --- a/Tests/test_file_avif.py +++ b/Tests/test_file_avif.py @@ -217,6 +217,52 @@ class TestFileAvif: with Image.open(blob) as im: im.load() + def test_save_all_progress(self) -> None: + out = BytesIO() + progress = [] + + def callback(state) -> None: + if state["image_filename"]: + state["image_filename"] = os.path.basename(state["image_filename"]) + progress.append(state) + + Image.new("RGB", (1, 1)).save(out, "AVIF", save_all=True, progress=callback) + assert progress == [ + { + "image_index": 0, + "image_filename": None, + "completed_frames": 1, + "total_frames": 1, + } + ] + + out = BytesIO() + progress = [] + + with Image.open("Tests/images/avif/star.avifs") as im: + im2 = Image.new(im.mode, im.size) + im.save(out, "AVIF", save_all=True, append_images=[im2], progress=callback) + + expected = [] + for i in range(5): + expected.append( + { + "image_index": 0, + "image_filename": "star.avifs", + "completed_frames": i + 1, + "total_frames": 6, + } + ) + expected.append( + { + "image_index": 1, + "image_filename": None, + "completed_frames": 6, + "total_frames": 6, + } + ) + assert progress == expected + def test_background_from_gif(self, tmp_path: Path) -> None: with Image.open("Tests/images/chi.gif") as im: original_value = im.convert("RGB").getpixel((1, 1)) diff --git a/Tests/test_file_gif.py b/Tests/test_file_gif.py index be1a2231b..dd67bb849 100644 --- a/Tests/test_file_gif.py +++ b/Tests/test_file_gif.py @@ -1,5 +1,6 @@ from __future__ import annotations +import os import warnings from collections.abc import Generator from io import BytesIO @@ -296,15 +297,13 @@ def test_roundtrip_save_all_1(tmp_path: Path) -> None: assert reloaded.getpixel((0, 0)) == 255 -def test_save_all_progress(): +def test_save_all_progress() -> None: out = BytesIO() progress = [] - def callback(state): + def callback(state) -> None: if state["image_filename"]: - state["image_filename"] = ( - state["image_filename"].replace("\\", "/").split("Tests/images/")[-1] - ) + state["image_filename"] = os.path.basename(state["image_filename"]) progress.append(state) Image.new("RGB", (1, 1)).save(out, "GIF", save_all=True, progress=callback) @@ -324,7 +323,7 @@ def test_save_all_progress(): im = Image.new("RGB", im2.size) im.save(out, "GIF", save_all=True, append_images=[im2], progress=callback) - expected = [ + expected: list[dict[str, int | str | None]] = [ { "image_index": 0, "image_filename": None, diff --git a/Tests/test_file_pdf.py b/Tests/test_file_pdf.py index c4aa999f9..e2973c888 100644 --- a/Tests/test_file_pdf.py +++ b/Tests/test_file_pdf.py @@ -178,11 +178,9 @@ def test_save_all_progress() -> None: out = BytesIO() progress = [] - def callback(state): + def callback(state) -> None: if state["image_filename"]: - state["image_filename"] = ( - state["image_filename"].replace("\\", "/").split("Tests/images/")[-1] - ) + state["image_filename"] = os.path.basename(state["image_filename"]) progress.append(state) Image.new("RGB", (1, 1)).save(out, "PDF", save_all=True, progress=callback) diff --git a/Tests/test_file_tiff.py b/Tests/test_file_tiff.py index 3a96f7553..2e6d7faf0 100644 --- a/Tests/test_file_tiff.py +++ b/Tests/test_file_tiff.py @@ -797,13 +797,9 @@ class TestFileTiff: out = BytesIO() progress = [] - def callback(state): + def callback(state) -> None: if state["image_filename"]: - state["image_filename"] = ( - state["image_filename"] - .replace("\\", "/") - .split("Tests/images/")[-1] - ) + state["image_filename"] = os.path.basename(state["image_filename"]) progress.append(state) Image.new("RGB", (1, 1)).save(out, "TIFF", save_all=True, progress=callback) diff --git a/Tests/test_file_webp.py b/Tests/test_file_webp.py index 5d45d5b3a..3e1aedda3 100644 --- a/Tests/test_file_webp.py +++ b/Tests/test_file_webp.py @@ -1,5 +1,6 @@ from __future__ import annotations +import os import re import sys import warnings @@ -129,18 +130,13 @@ class TestFileWebp: with pytest.raises(ValueError): _webp.WebPEncode(im.getim(), False, 0, 0, "", 4, 0, b"", "") - @skip_unless_feature("webp_anim") def test_save_all_progress(self) -> None: out = BytesIO() progress = [] - def callback(state): + def callback(state) -> None: if state["image_filename"]: - state["image_filename"] = ( - state["image_filename"] - .replace("\\", "/") - .split("Tests/images/")[-1] - ) + state["image_filename"] = os.path.basename(state["image_filename"]) progress.append(state) Image.new("RGB", (1, 1)).save(out, "WEBP", save_all=True, progress=callback) diff --git a/src/PIL/AvifImagePlugin.py b/src/PIL/AvifImagePlugin.py index b2c5ab15d..53b8b2305 100644 --- a/src/PIL/AvifImagePlugin.py +++ b/src/PIL/AvifImagePlugin.py @@ -236,8 +236,9 @@ def _save( frame_duration = 0 cur_idx = im.tell() is_single_frame = total == 1 + progress = info.get("progress") try: - for ims in [im] + append_images: + for i, ims in enumerate([im] + append_images): # Get number of frames in this image nfr = getattr(ims, "n_frames", 1) @@ -268,6 +269,7 @@ def _save( # Update frame index frame_idx += 1 + im._save_all_progress(progress, ims, i, frame_idx, total) if not save_all: break