Skip to content

Commit

Permalink
avif: If save_all is False only save a single frame
Browse files Browse the repository at this point in the history
  • Loading branch information
fdintino committed Feb 24, 2021
1 parent 4a70bc1 commit ff56a9c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
7 changes: 7 additions & 0 deletions Tests/test_file_avif.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
14 changes: 12 additions & 2 deletions src/PIL/AvifImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)

Expand All @@ -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")

0 comments on commit ff56a9c

Please sign in to comment.