mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-07-05 20:33:24 +03:00
Merge pull request #2406 from blochl/multiframe_tiff
Allow to save tiff stacks from separate images
This commit is contained in:
commit
1f19c023e1
|
@ -1742,17 +1742,28 @@ class AppendingTiffWriter:
|
||||||
|
|
||||||
|
|
||||||
def _save_all(im, fp, filename):
|
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)
|
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 idx in range(im.n_frames):
|
for ims in [im]+append_images:
|
||||||
im.seek(idx)
|
ims.encoderinfo = encoderinfo
|
||||||
im.load()
|
ims.encoderconfig = encoderconfig
|
||||||
_save(im, tf, filename)
|
if not hasattr(ims, "n_frames"):
|
||||||
tf.newFrame()
|
nfr = 1
|
||||||
|
else:
|
||||||
|
nfr = ims.n_frames
|
||||||
|
|
||||||
|
for idx in range(nfr):
|
||||||
|
ims.seek(idx)
|
||||||
|
ims.load()
|
||||||
|
_save(ims, tf, filename)
|
||||||
|
tf.newFrame()
|
||||||
finally:
|
finally:
|
||||||
im.seek(cur_idx)
|
im.seek(cur_idx)
|
||||||
|
|
||||||
|
|
|
@ -441,6 +441,18 @@ class TestFileTiff(PillowTestCase):
|
||||||
with Image.open(mp) as im:
|
with Image.open(mp) as im:
|
||||||
self.assertEqual(im.n_frames, 3)
|
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):
|
def test_saving_icc_profile(self):
|
||||||
# Tests saving TIFF with icc_profile set.
|
# Tests saving TIFF with icc_profile set.
|
||||||
# At the time of writing this will only work for non-compressed tiffs
|
# At the time of writing this will only work for non-compressed tiffs
|
||||||
|
|
Loading…
Reference in New Issue
Block a user