diff --git a/PIL/GifImagePlugin.py b/PIL/GifImagePlugin.py index 64764f99d..f227211a2 100644 --- a/PIL/GifImagePlugin.py +++ b/PIL/GifImagePlugin.py @@ -395,7 +395,7 @@ def _write_multiple_frames(im, fp, palette): im_frames = [] frame_count = 0 - for imSequence in [im]+im.encoderinfo.get("append_images", []): + for imSequence in itertools.chain([im], im.encoderinfo.get("append_images", [])): for im_frame in ImageSequence.Iterator(imSequence): # a copy is required here since seek can still mutate the image im_frame = _normalize_mode(im_frame.copy()) diff --git a/PIL/TiffImagePlugin.py b/PIL/TiffImagePlugin.py index 07fb97d14..1b60e8016 100644 --- a/PIL/TiffImagePlugin.py +++ b/PIL/TiffImagePlugin.py @@ -1785,13 +1785,13 @@ def _save_all(im, fp, filename): encoderinfo = im.encoderinfo.copy() encoderconfig = im.encoderconfig append_images = encoderinfo.get("append_images", []) - if not hasattr(im, "n_frames") and not len(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 [im]+append_images: + for ims in itertools.chain([im], append_images): ims.encoderinfo = encoderinfo ims.encoderconfig = encoderconfig if not hasattr(ims, "n_frames"): diff --git a/Tests/test_file_gif.py b/Tests/test_file_gif.py index 0b52d7b44..14ea83f28 100644 --- a/Tests/test_file_gif.py +++ b/Tests/test_file_gif.py @@ -410,8 +410,18 @@ class TestFileGif(PillowTestCase): # Test appending single frame images im = Image.new('RGB', (100, 100), '#f00') - ims = [Image.new('RGB', (100, 100), color) for color in ['#0f0', '#00f']] - im.save(out, save_all=True, append_images=ims) + ims = [Image.new('RGB', (100, 100), color) for color + in ['#0f0', '#00f']] + im.copy().save(out, save_all=True, append_images=ims) + + reread = Image.open(out) + self.assertEqual(reread.n_frames, 3) + + # Tests appending using a generator + def imGenerator(ims): + for im in ims: + yield im + im.save(out, save_all=True, append_images=imGenerator(ims)) reread = Image.open(out) self.assertEqual(reread.n_frames, 3) diff --git a/Tests/test_file_pdf.py b/Tests/test_file_pdf.py index 5cb04595a..fd65cc371 100644 --- a/Tests/test_file_pdf.py +++ b/Tests/test_file_pdf.py @@ -75,7 +75,17 @@ class TestFilePdf(PillowTestCase): self.assertGreater(os.path.getsize(outfile), 0) # Append images - im.save(outfile, save_all=True, append_images=[hopper()]) + ims = [hopper()] + im.copy().save(outfile, save_all=True, append_images=ims) + + self.assertTrue(os.path.isfile(outfile)) + self.assertGreater(os.path.getsize(outfile), 0) + + # Test appending using a generator + def imGenerator(ims): + for im in ims: + yield im + im.save(outfile, save_all=True, append_images=imGenerator(ims)) self.assertTrue(os.path.isfile(outfile)) self.assertGreater(os.path.getsize(outfile), 0) diff --git a/Tests/test_file_tiff.py b/Tests/test_file_tiff.py index 3ad8b2bd1..1eb784dfb 100644 --- a/Tests/test_file_tiff.py +++ b/Tests/test_file_tiff.py @@ -470,7 +470,18 @@ class TestFileTiff(PillowTestCase): im = Image.new('RGB', (100, 100), '#f00') ims = [Image.new('RGB', (100, 100), color) for color in ['#0f0', '#00f']] - im.save(mp, format="TIFF", save_all=True, append_images=ims) + im.copy().save(mp, format="TIFF", save_all=True, append_images=ims) + + mp.seek(0, os.SEEK_SET) + reread = Image.open(mp) + self.assertEqual(reread.n_frames, 3) + + # Test appending using a generator + def imGenerator(ims): + for im in ims: + yield im + mp = io.BytesIO() + im.save(mp, format="TIFF", save_all=True, append_images=imGenerator(ims)) mp.seek(0, os.SEEK_SET) reread = Image.open(mp)