From 3465a598e25383b78feca0bf6a2d942e2663bc95 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Mon, 2 Oct 2023 18:33:48 +1100 Subject: [PATCH] Pass back index of image in sequence, instead of filename --- Tests/test_file_apng.py | 16 ++++++++-------- Tests/test_file_gif.py | 6 +++--- Tests/test_file_mpo.py | 10 +++++----- Tests/test_file_pdf.py | 10 +++++----- Tests/test_file_tiff.py | 10 +++++----- Tests/test_file_webp.py | 6 +++--- src/PIL/GifImagePlugin.py | 8 +++----- src/PIL/MpoImagePlugin.py | 6 +++--- src/PIL/PdfImagePlugin.py | 6 ++---- src/PIL/PngImagePlugin.py | 8 +++----- src/PIL/TiffImagePlugin.py | 6 +++--- src/PIL/WebPImagePlugin.py | 6 +++--- 12 files changed, 46 insertions(+), 52 deletions(-) diff --git a/Tests/test_file_apng.py b/Tests/test_file_apng.py index d30df9dc7..da02b42b2 100644 --- a/Tests/test_file_apng.py +++ b/Tests/test_file_apng.py @@ -673,7 +673,7 @@ def test_save_all_progress(): progress.append((filename, frame_number, n_frames)) Image.new("RGB", (1, 1)).save(out, "PNG", save_all=True, progress=callback) - assert progress == [(None, 1, 1)] + assert progress == [(0, 1, 1)] out = BytesIO() progress = [] @@ -685,13 +685,13 @@ def test_save_all_progress(): ) assert progress == [ - ("Tests/images/apng/single_frame.png", 1, 7), - ("Tests/images/apng/single_frame.png", 2, 7), - ("Tests/images/apng/delay.png", 3, 7), - ("Tests/images/apng/delay.png", 4, 7), - ("Tests/images/apng/delay.png", 5, 7), - ("Tests/images/apng/delay.png", 6, 7), - ("Tests/images/apng/delay.png", 7, 7), + (0, 1, 7), + (1, 2, 7), + (2, 3, 7), + (2, 4, 7), + (2, 5, 7), + (2, 6, 7), + (2, 7, 7), ] diff --git a/Tests/test_file_gif.py b/Tests/test_file_gif.py index 9b2280dac..7478176dd 100644 --- a/Tests/test_file_gif.py +++ b/Tests/test_file_gif.py @@ -273,7 +273,7 @@ def test_save_all_progress(): progress.append((filename, frame_number, n_frames)) Image.new("RGB", (1, 1)).save(out, "GIF", save_all=True, progress=callback) - assert progress == [(None, 1, 1)] + assert progress == [(0, 1, 1)] out = BytesIO() progress = [] @@ -282,9 +282,9 @@ def test_save_all_progress(): with Image.open("Tests/images/chi.gif") as im2: im.save(out, "GIF", save_all=True, append_images=[im2], progress=callback) - expected = [("Tests/images/hopper.gif", 1, 32)] + expected = [(0, 1, 32)] for i in range(31): - expected.append(("Tests/images/chi.gif", i + 2, 32)) + expected.append((1, i + 2, 32)) assert progress == expected diff --git a/Tests/test_file_mpo.py b/Tests/test_file_mpo.py index 2497fe0af..bb6599296 100644 --- a/Tests/test_file_mpo.py +++ b/Tests/test_file_mpo.py @@ -288,7 +288,7 @@ def test_save_all_progress(): progress.append((filename, frame_number, n_frames)) Image.new("RGB", (1, 1)).save(out, "MPO", save_all=True, progress=callback) - assert progress == [(None, 1, 1)] + assert progress == [(0, 1, 1)] out = BytesIO() progress = [] @@ -298,8 +298,8 @@ def test_save_all_progress(): im.save(out, "MPO", save_all=True, append_images=[im2], progress=callback) assert progress == [ - ("Tests/images/sugarshack.mpo", 1, 4), - ("Tests/images/sugarshack.mpo", 2, 4), - ("Tests/images/frozenpond.mpo", 3, 4), - ("Tests/images/frozenpond.mpo", 4, 4), + (0, 1, 4), + (0, 2, 4), + (1, 3, 4), + (1, 4, 4), ] diff --git a/Tests/test_file_pdf.py b/Tests/test_file_pdf.py index 96ef787fd..71112ddb4 100644 --- a/Tests/test_file_pdf.py +++ b/Tests/test_file_pdf.py @@ -177,7 +177,7 @@ def test_save_all_progress(): progress.append((filename, frame_number, n_frames)) Image.new("RGB", (1, 1)).save(out, "PDF", save_all=True, progress=callback) - assert progress == [(None, 1, 1)] + assert progress == [(0, 1, 1)] out = BytesIO() progress = [] @@ -187,10 +187,10 @@ def test_save_all_progress(): im.save(out, "PDF", save_all=True, append_images=[im2], progress=callback) assert progress == [ - ("Tests/images/sugarshack.mpo", 1, 4), - ("Tests/images/sugarshack.mpo", 2, 4), - ("Tests/images/frozenpond.mpo", 3, 4), - ("Tests/images/frozenpond.mpo", 4, 4), + (0, 1, 4), + (0, 2, 4), + (1, 3, 4), + (1, 4, 4), ] diff --git a/Tests/test_file_tiff.py b/Tests/test_file_tiff.py index 1e8fa944e..e957f2371 100644 --- a/Tests/test_file_tiff.py +++ b/Tests/test_file_tiff.py @@ -696,7 +696,7 @@ class TestFileTiff: progress.append((filename, frame_number, n_frames)) Image.new("RGB", (1, 1)).save(out, "TIFF", save_all=True, progress=callback) - assert progress == [(None, 1, 1)] + assert progress == [(0, 1, 1)] out = BytesIO() progress = [] @@ -708,10 +708,10 @@ class TestFileTiff: ) assert progress == [ - ("Tests/images/hopper.tif", 1, 4), - ("Tests/images/multipage.tiff", 2, 4), - ("Tests/images/multipage.tiff", 3, 4), - ("Tests/images/multipage.tiff", 4, 4), + (0, 1, 4), + (1, 2, 4), + (1, 3, 4), + (1, 4, 4), ] def test_saving_icc_profile(self, tmp_path): diff --git a/Tests/test_file_webp.py b/Tests/test_file_webp.py index 1bfc9488b..420464e19 100644 --- a/Tests/test_file_webp.py +++ b/Tests/test_file_webp.py @@ -131,7 +131,7 @@ class TestFileWebp: progress.append((filename, frame_number, n_frames)) Image.new("RGB", (1, 1)).save(out, "WEBP", save_all=True, progress=callback) - assert progress == [(None, 1, 1)] + assert progress == [(0, 1, 1)] out = BytesIO() progress = [] @@ -142,8 +142,8 @@ class TestFileWebp: expected = [] for i in range(42): - expected.append(("Tests/images/iss634.webp", i + 1, 43)) - expected.append((None, 43, 43)) + expected.append((0, i + 1, 43)) + expected.append((1, 43, 43)) assert progress == expected def test_icc_profile(self, tmp_path): diff --git a/src/PIL/GifImagePlugin.py b/src/PIL/GifImagePlugin.py index d17d36319..7d2d9c3ae 100644 --- a/src/PIL/GifImagePlugin.py +++ b/src/PIL/GifImagePlugin.py @@ -587,7 +587,7 @@ def _write_multiple_frames(im, fp, palette): im_frames = [] frame_count = 0 background_im = None - for imSequence in imSequences: + for i, imSequence in enumerate(imSequences): for im_frame in ImageSequence.Iterator(imSequence): # a copy is required here since seek can still mutate the image im_frame = _normalize_mode(im_frame.copy()) @@ -618,9 +618,7 @@ def _write_multiple_frames(im, fp, palette): if encoderinfo.get("duration"): previous["encoderinfo"]["duration"] += encoderinfo["duration"] if progress: - progress( - getattr(imSequence, "filename", None), frame_count, n_frames - ) + progress(i, frame_count, n_frames) continue if encoderinfo.get("disposal") == 2: if background_im is None: @@ -635,7 +633,7 @@ def _write_multiple_frames(im, fp, palette): bbox = None im_frames.append({"im": im_frame, "bbox": bbox, "encoderinfo": encoderinfo}) if progress: - progress(getattr(imSequence, "filename", None), frame_count, n_frames) + progress(i, frame_count, n_frames) if len(im_frames) > 1: for frame_data in im_frames: diff --git a/src/PIL/MpoImagePlugin.py b/src/PIL/MpoImagePlugin.py index de9e6b74e..4a9498c8b 100644 --- a/src/PIL/MpoImagePlugin.py +++ b/src/PIL/MpoImagePlugin.py @@ -51,7 +51,7 @@ def _save_all(im, fp, filename): if not animated: _save(im, fp, filename) if progress: - progress(getattr(im, "filename", None), 1, 1) + progress(0, 1, 1) return mpf_offset = 28 @@ -62,7 +62,7 @@ def _save_all(im, fp, filename): n_frames = 0 for imSequence in imSequences: n_frames += getattr(imSequence, "n_frames", 1) - for imSequence in imSequences: + for i, imSequence in enumerate(imSequences): for im_frame in ImageSequence.Iterator(imSequence): if not offsets: # APP2 marker @@ -83,7 +83,7 @@ def _save_all(im, fp, filename): offsets.append(fp.tell() - offsets[-1]) if progress: frame_number += 1 - progress(getattr(imSequence, "filename", None), frame_number, n_frames) + progress(i, frame_number, n_frames) ifd = TiffImagePlugin.ImageFileDirectory_v2() ifd[0xB000] = b"0100" diff --git a/src/PIL/PdfImagePlugin.py b/src/PIL/PdfImagePlugin.py index 5e230e7f4..27e79bd8b 100644 --- a/src/PIL/PdfImagePlugin.py +++ b/src/PIL/PdfImagePlugin.py @@ -248,7 +248,7 @@ def _save(im, fp, filename, save_all=False): progress = im.encoderinfo.get("progress") page_number = 0 - for im_sequence in ims: + for i, im_sequence in enumerate(ims): im_pages = ImageSequence.Iterator(im_sequence) if save_all else [im_sequence] for im in im_pages: image_ref, procset = _write_image(im, filename, existing_pdf, image_refs) @@ -283,9 +283,7 @@ def _save(im, fp, filename, save_all=False): page_number += 1 if progress: - progress( - getattr(im_sequence, "filename", None), page_number, number_of_pages - ) + progress(i, page_number, number_of_pages) # # trailer diff --git a/src/PIL/PngImagePlugin.py b/src/PIL/PngImagePlugin.py index 1dec51f5a..8d2173dc7 100644 --- a/src/PIL/PngImagePlugin.py +++ b/src/PIL/PngImagePlugin.py @@ -1104,7 +1104,7 @@ def _write_multiple_frames(im, fp, chunk, rawmode, default_image, append_images) im_frames = [] frame_count = 0 - for imSequence in imSequences: + for i, imSequence in enumerate(imSequences): for im_frame in ImageSequence.Iterator(imSequence): if im_frame.mode == rawmode: im_frame = im_frame.copy() @@ -1155,9 +1155,7 @@ def _write_multiple_frames(im, fp, chunk, rawmode, default_image, append_images) "duration", duration ) if progress: - progress( - getattr(imSequence, "filename", None), frame_count, n_frames - ) + progress(i, frame_count, n_frames) continue else: bbox = None @@ -1165,7 +1163,7 @@ def _write_multiple_frames(im, fp, chunk, rawmode, default_image, append_images) encoderinfo["duration"] = duration im_frames.append({"im": im_frame, "bbox": bbox, "encoderinfo": encoderinfo}) if progress: - progress(getattr(imSequence, "filename", None), frame_count, n_frames) + progress(i, frame_count, n_frames) # animation control chunk( diff --git a/src/PIL/TiffImagePlugin.py b/src/PIL/TiffImagePlugin.py index d6e718ee9..71af42b75 100644 --- a/src/PIL/TiffImagePlugin.py +++ b/src/PIL/TiffImagePlugin.py @@ -2122,7 +2122,7 @@ def _save_all(im, fp, filename): if not hasattr(im, "n_frames") and not append_images: _save(im, fp, filename) if progress: - progress(getattr(im, "filename", None), 1, 1) + progress(0, 1, 1) return cur_idx = im.tell() @@ -2134,7 +2134,7 @@ def _save_all(im, fp, filename): n_frames += getattr(ims, "n_frames", 1) try: with AppendingTiffWriter(fp) as tf: - for ims in imSequences: + for i, ims in enumerate(imSequences): ims.encoderinfo = encoderinfo ims.encoderconfig = encoderconfig if not hasattr(ims, "n_frames"): @@ -2148,7 +2148,7 @@ def _save_all(im, fp, filename): _save(ims, tf, filename) if progress: frame_number += 1 - progress(getattr(ims, "filename", None), frame_number, n_frames) + progress(i, frame_number, n_frames) tf.newFrame() finally: diff --git a/src/PIL/WebPImagePlugin.py b/src/PIL/WebPImagePlugin.py index 26f5704b8..c3113ea89 100644 --- a/src/PIL/WebPImagePlugin.py +++ b/src/PIL/WebPImagePlugin.py @@ -188,7 +188,7 @@ def _save_all(im, fp, filename): if total == 1: _save(im, fp, filename) if progress: - progress(getattr(im, "filename", None), 1, 1) + progress(0, 1, 1) return background = (0, 0, 0, 0) @@ -261,7 +261,7 @@ def _save_all(im, fp, filename): timestamp = 0 cur_idx = im.tell() try: - for ims in [im] + append_images: + for i, ims in enumerate([im] + append_images): # Get # of frames in this image nfr = getattr(ims, "n_frames", 1) @@ -304,7 +304,7 @@ def _save_all(im, fp, filename): timestamp += duration frame_idx += 1 if progress: - progress(getattr(ims, "filename", None), frame_idx, total) + progress(i, frame_idx, total) finally: im.seek(cur_idx)