From b4335710548ae5abd8fab98e1d6b4c9498164b1c Mon Sep 17 00:00:00 2001 From: Frankie Dintino Date: Tue, 23 Feb 2021 18:59:06 -0500 Subject: [PATCH] avif: If save_all is False only save a single frame --- Tests/test_file_avif.py | 7 +++++++ src/PIL/AvifImagePlugin.py | 14 ++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/Tests/test_file_avif.py b/Tests/test_file_avif.py index f9f2d84adef..c0165cc9223 100644 --- a/Tests/test_file_avif.py +++ b/Tests/test_file_avif.py @@ -179,6 +179,13 @@ def test_background_from_gif(self, tmp_path): ) assert difference < 5 + def test_save_single_frame(self, tmp_path): + temp_file = str(tmp_path / "temp.avif") + with Image.open("Tests/images/chi.gif") as im: + im.save(temp_file) + with Image.open(temp_file) as im: + assert im.n_frames == 1 + def test_invalid_file(self): invalid_file = "Tests/images/flower.jpg" diff --git a/src/PIL/AvifImagePlugin.py b/src/PIL/AvifImagePlugin.py index 136f4d519ac..c0b30058a65 100644 --- a/src/PIL/AvifImagePlugin.py +++ b/src/PIL/AvifImagePlugin.py @@ -85,8 +85,15 @@ def tell(self): def _save_all(im, fp, filename): + _save(im, fp, filename, save_all=True) + + +def _save(im, fp, filename, save_all=False): info = im.encoderinfo.copy() - append_images = list(info.get("append_images", [])) + if save_all: + append_images = list(info.get("append_images", [])) + else: + append_images = [] total = 0 for ims in [im] + append_images: @@ -186,6 +193,9 @@ def _save_all(im, fp, filename): # Update frame index frame_idx += 1 + if not save_all: + break + finally: im.seek(cur_idx) @@ -199,7 +209,7 @@ def _save_all(im, fp, filename): Image.register_open(AvifImageFile.format, AvifImageFile, _accept) if SUPPORTED: - Image.register_save(AvifImageFile.format, _save_all) + Image.register_save(AvifImageFile.format, _save) Image.register_save_all(AvifImageFile.format, _save_all) Image.register_extensions(AvifImageFile.format, [".avif", ".avifs"]) Image.register_mime(AvifImageFile.format, "image/avif")