Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for generators when using append_images for WEBP #2835

Merged
merged 3 commits into from
Nov 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions PIL/TiffImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1784,14 +1784,14 @@ def fixOffsets(self, count, isShort=False, isLong=False):
def _save_all(im, fp, filename):
encoderinfo = im.encoderinfo.copy()
encoderconfig = im.encoderconfig
append_images = encoderinfo.get("append_images", [])
append_images = list(encoderinfo.get("append_images", []))
if not hasattr(im, "n_frames") and not append_images:
return _save(im, fp, filename)

cur_idx = im.tell()
try:
with AppendingTiffWriter(fp) as tf:
for ims in itertools.chain([im], append_images):
for ims in [im]+append_images:
ims.encoderinfo = encoderinfo
ims.encoderconfig = encoderconfig
if not hasattr(ims, "n_frames"):
Expand Down
2 changes: 1 addition & 1 deletion PIL/WebPImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def tell(self):

def _save_all(im, fp, filename):
encoderinfo = im.encoderinfo.copy()
append_images = encoderinfo.get("append_images", [])
append_images = list(encoderinfo.get("append_images", []))

# If total frame count is 1, then save using the legacy API, which
# will preserve non-alpha modes
Expand Down
42 changes: 26 additions & 16 deletions Tests/test_file_webp_animated.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,25 +65,35 @@ def test_write_animation_RGB(self):
are visually similar to the originals.
"""

temp_file = self.tempfile("temp.webp")
temp_file2 = self.tempfile("temp.png")
frame1 = Image.open('Tests/images/anim_frame1.webp')
frame2 = Image.open('Tests/images/anim_frame2.webp')
frame1.save(temp_file,
save_all=True, append_images=[frame2], lossless=True)
def check(temp_file):
im = Image.open(temp_file)
self.assertEqual(im.n_frames, 2)

im = Image.open(temp_file)
self.assertEqual(im.n_frames, 2)
# Compare first frame to original
im.load()
self.assert_image_equal(im, frame1.convert("RGBA"))

# Compare first frame to original
im.load()
im.save(temp_file2)
self.assert_image_equal(im, frame1.convert("RGBA"))
# Compare second frame to original
im.seek(1)
im.load()
self.assert_image_equal(im, frame2.convert("RGBA"))

# Compare second frame to original
im.seek(1)
im.load()
self.assert_image_equal(im, frame2.convert("RGBA"))
frame1 = Image.open('Tests/images/anim_frame1.webp')
frame2 = Image.open('Tests/images/anim_frame2.webp')

temp_file1 = self.tempfile("temp.webp")
frame1.copy().save(temp_file1,
save_all=True, append_images=[frame2], lossless=True)
check(temp_file1)

# Tests appending using a generator
def imGenerator(ims):
for im in ims:
yield im
temp_file2 = self.tempfile("temp_generator.webp")
frame1.copy().save(temp_file2,
save_all=True, append_images=imGenerator([frame2]), lossless=True)
check(temp_file2)

def test_timestamp_and_duration(self):
"""
Expand Down