Merge pull request #2829 from radarhere/generator

Added support for generators when using append_images
This commit is contained in:
wiredfool 2017-11-05 14:59:02 +00:00 committed by GitHub
commit e341fbfc05
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 38 additions and 7 deletions

View File

@ -395,7 +395,7 @@ def _write_multiple_frames(im, fp, palette):
im_frames = [] im_frames = []
frame_count = 0 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): for im_frame in ImageSequence.Iterator(imSequence):
# a copy is required here since seek can still mutate the image # a copy is required here since seek can still mutate the image
im_frame = _normalize_mode(im_frame.copy()) im_frame = _normalize_mode(im_frame.copy())

View File

@ -1785,13 +1785,13 @@ 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 = 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) 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 [im]+append_images: for ims in itertools.chain([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"):

View File

@ -410,8 +410,18 @@ class TestFileGif(PillowTestCase):
# Test appending single frame images # Test appending single frame images
im = Image.new('RGB', (100, 100), '#f00') im = Image.new('RGB', (100, 100), '#f00')
ims = [Image.new('RGB', (100, 100), color) for color in ['#0f0', '#00f']] ims = [Image.new('RGB', (100, 100), color) for color
im.save(out, save_all=True, append_images=ims) 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) reread = Image.open(out)
self.assertEqual(reread.n_frames, 3) self.assertEqual(reread.n_frames, 3)

View File

@ -75,7 +75,17 @@ class TestFilePdf(PillowTestCase):
self.assertGreater(os.path.getsize(outfile), 0) self.assertGreater(os.path.getsize(outfile), 0)
# Append images # 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.assertTrue(os.path.isfile(outfile))
self.assertGreater(os.path.getsize(outfile), 0) self.assertGreater(os.path.getsize(outfile), 0)

View File

@ -470,7 +470,18 @@ class TestFileTiff(PillowTestCase):
im = Image.new('RGB', (100, 100), '#f00') im = Image.new('RGB', (100, 100), '#f00')
ims = [Image.new('RGB', (100, 100), color) for color ims = [Image.new('RGB', (100, 100), color) for color
in ['#0f0', '#00f']] 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) mp.seek(0, os.SEEK_SET)
reread = Image.open(mp) reread = Image.open(mp)