mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-12-26 01:46:18 +03:00
Added support for generators when using append_images
This commit is contained in:
parent
ccd28fbc19
commit
bc255c97ff
|
@ -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())
|
||||
|
|
|
@ -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"):
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue
Block a user