Merge pull request #2406 from blochl/multiframe_tiff

Allow to save tiff stacks from separate images
This commit is contained in:
wiredfool 2017-06-13 22:30:35 +01:00 committed by GitHub
commit 1f19c023e1
2 changed files with 29 additions and 6 deletions

View File

@ -1742,17 +1742,28 @@ class AppendingTiffWriter:
def _save_all(im, fp, filename):
if not hasattr(im, "n_frames"):
encoderinfo = im.encoderinfo.copy()
encoderconfig = im.encoderconfig
append_images = encoderinfo.get("append_images", [])
if not hasattr(im, "n_frames") and not len(append_images):
return _save(im, fp, filename)
cur_idx = im.tell()
try:
with AppendingTiffWriter(fp) as tf:
for idx in range(im.n_frames):
im.seek(idx)
im.load()
_save(im, tf, filename)
tf.newFrame()
for ims in [im]+append_images:
ims.encoderinfo = encoderinfo
ims.encoderconfig = encoderconfig
if not hasattr(ims, "n_frames"):
nfr = 1
else:
nfr = ims.n_frames
for idx in range(nfr):
ims.seek(idx)
ims.load()
_save(ims, tf, filename)
tf.newFrame()
finally:
im.seek(cur_idx)

View File

@ -441,6 +441,18 @@ class TestFileTiff(PillowTestCase):
with Image.open(mp) as im:
self.assertEqual(im.n_frames, 3)
# Test appending images
mp = io.BytesIO()
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)
mp.seek(0, os.SEEK_SET)
reread = Image.open(mp)
self.assertEqual(reread.n_frames, 3)
def test_saving_icc_profile(self):
# Tests saving TIFF with icc_profile set.
# At the time of writing this will only work for non-compressed tiffs