mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-07-16 19:22:39 +03:00
Merge pull request #2835 from radarhere/generator
Added support for generators when using append_images for WEBP
This commit is contained in:
commit
669fd6a600
|
@ -1784,14 +1784,14 @@ class AppendingTiffWriter:
|
||||||
def _save_all(im, fp, filename):
|
def _save_all(im, fp, filename):
|
||||||
encoderinfo = im.encoderinfo.copy()
|
encoderinfo = im.encoderinfo.copy()
|
||||||
encoderconfig = im.encoderconfig
|
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:
|
if not hasattr(im, "n_frames") and not append_images:
|
||||||
return _save(im, fp, filename)
|
return _save(im, fp, filename)
|
||||||
|
|
||||||
cur_idx = im.tell()
|
cur_idx = im.tell()
|
||||||
try:
|
try:
|
||||||
with AppendingTiffWriter(fp) as tf:
|
with AppendingTiffWriter(fp) as tf:
|
||||||
for ims in itertools.chain([im], append_images):
|
for ims in [im]+append_images:
|
||||||
ims.encoderinfo = encoderinfo
|
ims.encoderinfo = encoderinfo
|
||||||
ims.encoderconfig = encoderconfig
|
ims.encoderconfig = encoderconfig
|
||||||
if not hasattr(ims, "n_frames"):
|
if not hasattr(ims, "n_frames"):
|
||||||
|
|
|
@ -167,7 +167,7 @@ class WebPImageFile(ImageFile.ImageFile):
|
||||||
|
|
||||||
def _save_all(im, fp, filename):
|
def _save_all(im, fp, filename):
|
||||||
encoderinfo = im.encoderinfo.copy()
|
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
|
# If total frame count is 1, then save using the legacy API, which
|
||||||
# will preserve non-alpha modes
|
# will preserve non-alpha modes
|
||||||
|
|
|
@ -65,25 +65,35 @@ class TestFileWebpAnimation(PillowTestCase):
|
||||||
are visually similar to the originals.
|
are visually similar to the originals.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
temp_file = self.tempfile("temp.webp")
|
def check(temp_file):
|
||||||
temp_file2 = self.tempfile("temp.png")
|
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 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')
|
frame1 = Image.open('Tests/images/anim_frame1.webp')
|
||||||
frame2 = Image.open('Tests/images/anim_frame2.webp')
|
frame2 = Image.open('Tests/images/anim_frame2.webp')
|
||||||
frame1.save(temp_file,
|
|
||||||
|
temp_file1 = self.tempfile("temp.webp")
|
||||||
|
frame1.copy().save(temp_file1,
|
||||||
save_all=True, append_images=[frame2], lossless=True)
|
save_all=True, append_images=[frame2], lossless=True)
|
||||||
|
check(temp_file1)
|
||||||
|
|
||||||
im = Image.open(temp_file)
|
# Tests appending using a generator
|
||||||
self.assertEqual(im.n_frames, 2)
|
def imGenerator(ims):
|
||||||
|
for im in ims:
|
||||||
# Compare first frame to original
|
yield im
|
||||||
im.load()
|
temp_file2 = self.tempfile("temp_generator.webp")
|
||||||
im.save(temp_file2)
|
frame1.copy().save(temp_file2,
|
||||||
self.assert_image_equal(im, frame1.convert("RGBA"))
|
save_all=True, append_images=imGenerator([frame2]), lossless=True)
|
||||||
|
check(temp_file2)
|
||||||
# Compare second frame to original
|
|
||||||
im.seek(1)
|
|
||||||
im.load()
|
|
||||||
self.assert_image_equal(im, frame2.convert("RGBA"))
|
|
||||||
|
|
||||||
def test_timestamp_and_duration(self):
|
def test_timestamp_and_duration(self):
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in New Issue
Block a user