Writing multipage TIFF with Python
An easy, robust way to write multipage TIFF on any platform in Python is imageio.
For all examples below, assume a stack of images in an Numpy ndarray imgs, with dimensions:
imgs.shape- (Nimg, y, x) for monochrome. (Nimg, y, x, 3) for RGB color.
ImageIO is a library we have contributed code to and recommend in general for Python image IO.
pip install imageioimport imageio
imageio.mimwrite('myimgs.tiff',imgs)tifffile
ImageIO uses tifffile internally, so most don’t need to use tifffile directly.
To use tifffile directly, install
tifffile.py:
pip install tifffileimport tifffile
tifffile.imsave('myimages.tiff',imgs)tifffile.imsave() is capable of description and tags arguments and to compress losslessly.
Advanced Python TIFF multi-page writing example: archive/old_tiffile_demo.py.
Read TIFF headers
The de facto TIFF header tags. can be read from the command line with Perl Image::ExifTool
apt install libimage-exiftool-perl
exiftool myfile.tifNote: tiffinfo doesn’t print tiff tags.
Print all TIFF tags from Python using archive/PrintTiffTags.py
Alternative multipage-Tiff method using scikit-image and FreeImage: (we recommend imageio or tifffile instead)
from skimage.io._plugins import freeimage_plugin as freeimg
freeimg.write_multipage(imgs,'myimages.tiff')Due to the large number of image libraries invoked, sometimes scikit-image needs a little tweaking for image I/O:
Windows
if you get:
RuntimeError: Could not find a FreeImage library
Fix by downloading the
FreeImage binaries
and extract Dist/x64/FreeImage.dll to the directory found by:
$(python -c "import skimage; print(skimage.__path__[0])")/io/_plugins/Linux
If you get:
freeimage had a problem: Could not find a FreeImage library in any of...
Fix by:
apt install libfreeimage3Old Numpy
To fix tifffile error caused by too-old Numpy version:
RuntimeError: module compiled against API version 0xb but this version of numpy is 0xa
Install a newer Numpy version:
pip install numpy
pip install tifffileSince there is a Numpy .whl binary wheel for ARM, the latest Numpy installs quickly on Raspberry Pi.