Writing multipage TIFF with Python
The best/easiest way to write multipage TIFF on any platform (Windows, Mac, Linux) is imageio
.
In all the examples:
imgs.shape
- (Nimg,y,x) for monochrome. (Nimg,y,x,3) for RGB color.
python -m pip install imageio
import imageio
imageio.mimwrite('myimgs.tiff',imgs)
tifffile
The next best way to write multipage TIFF from Python is tifffile
, which imageio
uses internally.
Install tifffile.py:
python -m pip install tifffile
Example
import 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: Demo_image_write_multipage.py.
Raspberry Pi
tifffile ≥ 0.7 requires Numpy 1.13, while the Raspberry Pi has Numpy 1.12 with most up to date Raspbian 9.1 currently available.
I worked around this issue by
python -m pip install numpy
python -m pip install tifffile
Since there is a Numpy .whl
for ARM, the latest Numpy installs quickly on Raspberry Pi.
This fixes tifffile
error:
RuntimeError: module compiled against API version 0xb but this version of numpy is 0xa
Read TIFF headers
There are de facto TIFF header tags. These can be read from the command line with
apt install libimage-exiftool-perl
exiftool myfile.tif
Note: tiffinfo
doesn’t print tiff tags
Print all TIFF tags from Python using PrintTiffTags.py
Other methods
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')
Fix scikit-image errors
Due to the large number of image libraries invoked, sometimes scikit-image
needs a little tweaking for image I/O:
Windows
if you get error:
RuntimeError: Could not find a FreeImage library
Fix by:
- download the FreeImage binaries
- extract
Dist/x64/FreeImage.dll
to$(python -c "import skimage; print(skimage.__path__[0])")/io/_plugins/
Linux
If you get error:
freeimage had a problem: Could not find a FreeImage library in any of…
Fix by:
apt install libfreeimage3
Leave a Comment