From 6c6f95f1d646dfe702b7dde40eec37a665813bdf Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Mon, 6 Nov 2017 19:54:15 +1100 Subject: [PATCH 1/3] Removed unnecessary code --- Tests/test_file_webp_animated.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/Tests/test_file_webp_animated.py b/Tests/test_file_webp_animated.py index ba5d4b3af..516cb1b7f 100644 --- a/Tests/test_file_webp_animated.py +++ b/Tests/test_file_webp_animated.py @@ -66,7 +66,6 @@ class TestFileWebpAnimation(PillowTestCase): """ 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, @@ -77,7 +76,6 @@ class TestFileWebpAnimation(PillowTestCase): # Compare first frame to original im.load() - im.save(temp_file2) self.assert_image_equal(im, frame1.convert("RGBA")) # Compare second frame to original From bfaa0a1f072af1dfb3f56040758cf418949b99ad Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Mon, 6 Nov 2017 20:06:50 +1100 Subject: [PATCH 2/3] Added support for generators when using append_images for WEBP --- PIL/WebPImagePlugin.py | 2 +- Tests/test_file_webp_animated.py | 38 +++++++++++++++++++++----------- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/PIL/WebPImagePlugin.py b/PIL/WebPImagePlugin.py index 36579dddd..39a8f2e35 100644 --- a/PIL/WebPImagePlugin.py +++ b/PIL/WebPImagePlugin.py @@ -167,7 +167,7 @@ class WebPImageFile(ImageFile.ImageFile): 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 diff --git a/Tests/test_file_webp_animated.py b/Tests/test_file_webp_animated.py index 516cb1b7f..f98cde764 100644 --- a/Tests/test_file_webp_animated.py +++ b/Tests/test_file_webp_animated.py @@ -65,23 +65,35 @@ class TestFileWebpAnimation(PillowTestCase): are visually similar to the originals. """ - temp_file = self.tempfile("temp.webp") + def check(temp_file): + 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') 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) + check(temp_file1) - 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")) + # 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): """ From 04d9ce29a9ff978892bce3ec99bc76c9ee64d1fb Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Mon, 6 Nov 2017 20:11:29 +1100 Subject: [PATCH 3/3] Changed TIFF saving to use single frame save if given a single frame image and an empty generator --- PIL/TiffImagePlugin.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/PIL/TiffImagePlugin.py b/PIL/TiffImagePlugin.py index 1b60e8016..b9dd19a61 100644 --- a/PIL/TiffImagePlugin.py +++ b/PIL/TiffImagePlugin.py @@ -1784,14 +1784,14 @@ class AppendingTiffWriter: 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"):