mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-08-18 19:24:47 +03:00
Allow to save tiff stacks from separate images
This is a quick solution that will allow to save tiff stacks from separate images, e.g. from Numpy arrays. Previously, tiff stacks could be saved only from multiframe images. This behavior is similar to what is possible now with GIFs. Example: import numpy as np from PIL import Image a = np.ones((100,100,100), dtype=np.uint8) imlist = [] for m in a: imlist.append(Image.fromarray(m)) imlist[0].save("test.tif", compression="tiff_deflate", save_all=True, append_images=imlist[1:]) (Should result in a 100-frame, 100x100 tiff stack.) Signed-off-by: Leonid Bloch <leonid.bloch@esrf.fr>
This commit is contained in:
parent
0f9233623e
commit
4b6fb417a0
|
@ -1737,17 +1737,28 @@ class AppendingTiffWriter:
|
||||||
self.rewriteLastLong(offset)
|
self.rewriteLastLong(offset)
|
||||||
|
|
||||||
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)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user