Tjelvar Olsson     About     Posts     Feed     Newsletter

Saving 16-bit tiff files using Python

When dealing with microscopy data it is not uncommon to be dealing with image files that have 16-bit channels. This presents a difficulty when working with Python as many imaging libraries struggle to save numpy.uint16 arrays.

To illustrate the problem let us create a white 50x50 pixel 16-bit image using numpy.

>>> import numpy as np
>>> ar = np.ones((50,50), dtype=np.uint16)
>>> ar = ar * np.iinfo(np.uint16).max

PIL/Pillow simply, and helpfully, raises a TypeError.

>>> from PIL import Image
>>> img = Image.fromarray(ar)
Traceback (most recent call last):
TypeError: Cannot handle this data type

SciPy does save the file, but it converts it to 8-bit. Personally I do not like this behaviour as it has caused me confusion on several occasions as subsequent steps of the analysis has read the file and tried to extract meaningful information from it.

>>> import scipy.misc
>>> scipy.misc.imsave('scipy.tiff', ar)
>>> ar2 = scipy.misc.imread('scipy.tiff')
>>> ar2.dtype
>>> np.max(ar2)

PyLibTiff to the rescue

PyLibTiff is a package that provides a wrapper to the libtiff library. To use it simply make sure that you have the libtiff library installed on your system and then you can use pip to install PyLibTiff. On a Debian based system.

sudo apt-get install libtiff-dev
sudo pip install libtiff

Now let us look at how to save a file using PyLibTiff.

>>> from libtiff import TIFF
>>> tiff ='libtiff.tiff', mode='w')
>>> tiff.write_image(ar)
>>> tiff.close()

To show that everything is working as expected let us open the tiff file and read in the image from it.

>>> tiff ='libtiff.tiff', mode='r')
>>> ar = tiff.read_image()
>>> tiff.close()
>>> ar.dtype
>>> np.max(ar)

Other options

Another option for working with 16-bit tiff files is OpenCV-Python. I also believe that can handle them, although I have not tested this myself. The reason I prefer PyLibTiff over these is that it can be installed into a virtual environment using pip.