Skip to content

Commit b433571

Browse files
committed
avif: If save_all is False only save a single frame
1 parent 4a70bc1 commit b433571

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

Tests/test_file_avif.py

+7
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,13 @@ def test_background_from_gif(self, tmp_path):
179179
)
180180
assert difference < 5
181181

182+
def test_save_single_frame(self, tmp_path):
183+
temp_file = str(tmp_path / "temp.avif")
184+
with Image.open("Tests/images/chi.gif") as im:
185+
im.save(temp_file)
186+
with Image.open(temp_file) as im:
187+
assert im.n_frames == 1
188+
182189
def test_invalid_file(self):
183190
invalid_file = "Tests/images/flower.jpg"
184191

src/PIL/AvifImagePlugin.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,15 @@ def tell(self):
8585

8686

8787
def _save_all(im, fp, filename):
88+
_save(im, fp, filename, save_all=True)
89+
90+
91+
def _save(im, fp, filename, save_all=False):
8892
info = im.encoderinfo.copy()
89-
append_images = list(info.get("append_images", []))
93+
if save_all:
94+
append_images = list(info.get("append_images", []))
95+
else:
96+
append_images = []
9097

9198
total = 0
9299
for ims in [im] + append_images:
@@ -186,6 +193,9 @@ def _save_all(im, fp, filename):
186193
# Update frame index
187194
frame_idx += 1
188195

196+
if not save_all:
197+
break
198+
189199
finally:
190200
im.seek(cur_idx)
191201

@@ -199,7 +209,7 @@ def _save_all(im, fp, filename):
199209

200210
Image.register_open(AvifImageFile.format, AvifImageFile, _accept)
201211
if SUPPORTED:
202-
Image.register_save(AvifImageFile.format, _save_all)
212+
Image.register_save(AvifImageFile.format, _save)
203213
Image.register_save_all(AvifImageFile.format, _save_all)
204214
Image.register_extensions(AvifImageFile.format, [".avif", ".avifs"])
205215
Image.register_mime(AvifImageFile.format, "image/avif")

0 commit comments

Comments
 (0)