Thursday, January 29, 2015

Using PyLibTiff to read tiff files in Python

ImageJ can open and save .tiff image stacks with ease. Additionally, to use BrainAligner to register image stacks, it is useful to have the image stacks saved as .tiff files. (My post about BrainAligner is here.) Various Python libraries exist that can open .tiff files, but some of them cannot handle image stacks (3d arrays of pixels). I've started to use PyLibTiff to handle this file type in my Python scripts. In order to install it, just run

sudo apt-get install python-libtiff

at a terminal.

Update: This does not appear to work in Ubuntu 14.04. It results in installation of version 0.3.0, which I believe has been replaced by 0.4.0. You can download the newest version at then unpack it, cd to the directory, and run

sudo python install

For some reason you will need to move out of the downloaded directory in order to actually test it, though. Open a python interactive prompt and make sure that you can import it: from libtiff import TIFF
Below is a snippet of some code that I've been using to open a 2-channel (2-color) image stack and arrange the dimensions in a way that I find intuitive (x,y,z,color).


Unknown said...

No dice:

cornelis@Bob:~/Downloads/libtiff-0.4.0$ sudo python install
Warning: Assuming default configuration (libtiff/bitarray-0.3.5-numpy/bitarray/{setup_bitarray,setup}.py was not found)Appending libtiff.bitarray configuration to libtiff
Ignoring attempt to set 'name' (from 'libtiff' to 'libtiff.bitarray')
/bin/sh: 1: svnversion: not found
Appending libtiff configuration to
Ignoring attempt to set 'name' (from '' to 'libtiff')
running install
running build
running config_cc
unifing config_cc, config, build_clib, build_ext, build commands --compiler options
running config_fc
unifing config_fc, config, build_clib, build_ext, build commands --fcompiler options
running build_src
building extension "libtiff.bitarray._bitarray" sources
building extension "libtiff.bittools" sources
building extension "libtiff.tif_lzw" sources
building data_files sources
build_src: building npy-pkg config files
running build_py
copying libtiff/ -> build/lib.linux-x86_64-2.7/libtiff
running build_ext
customize UnixCCompiler
customize UnixCCompiler using build_ext
running install_lib
copying build/lib.linux-x86_64-2.7/libtiff/ -> /usr/local/lib/python2.7/dist-packages/libtiff
byte-compiling /usr/local/lib/python2.7/dist-packages/libtiff/ to version.pyc
running install_data
running install_egg_info
Removing /usr/local/lib/python2.7/dist-packages/libtiff-0.4.0.egg-info
Writing /usr/local/lib/python2.7/dist-packages/libtiff-0.4.0.egg-info
running install_clib
customize UnixCCompiler
cornelis@Bob:~/Downloads/libtiff-0.4.0$ cd ..
cornelis@Bob:~/Downloads$ python
Python 3.5.1 |Anaconda 2.4.1 (64-bit)| (default, Dec 7 2015, 11:16:01)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from libtiff import TIFF
Traceback (most recent call last):
File "", line 1, in
ImportError: No module named 'libtiff'

Peter Weir said...

hmm, strange. I haven't run into that problem. One thing in your log: it looks like pylibtiff is getting installed in python2.7, whereas your interactive prompt indicates python 3.5.1?

DevRabbit IT Solutions Inc. said...

The Article on Using PyLibTiff to read tiff files in Python is give detail information about it .Thanks for Sharing the information about Python. data science consulting