ScanImage Tiff Reader for Python


The libarary is pip-installable for 64-bit Windows, OS X, or Linux. We test against python 3.6 and python 2.7.14.

pip install scanimage-tiff-reader


Read a volume from a tiff stack:

from ScanImageTiffReader import ScanImageTiffReader


The ScanImageTiffReader reads data from Tiff and BigTiff files recorded using ScanImage. It was written with performance in mind and provides access to ScanImage-specific metadata. It is also available for Matlab, Julia and C. There’s also a command line interface. This library should actually work with most tiff files, but as of now we don’t support compressed or tiled data.

The library is pip-installable for 64-bit Windows, OS X, or Linux. We test against python 3.6 and python 2.7.14.

Both ScanImage and this reader are products of Vidrio Technologies. If you have questions or need support feel free to submit an issue or contact us.

ScanImage Tiff Files

ScanImage records images, volumes and video as Tiff or BigTiff files. For the most part Tiff and BigTiff are similar, but they are not the same; BigTiff enables storage of larger (>4GB) data sets. In addition to the image data, ScanImage stores metadata describing the microscope configuration and settings used during the acquisition. These are stored in the file itself.

Some of this metadata is accessible using standard Tiff readers. The tiff format provides for data fields (tags) which can be used to attach data to describing each frame. Past versions of ScanImage would store a copy the metadata in “image description” tag for each frame. Much of the metadata is redundant, and this can lead to longer load times and significant storage overhead in some cases.

Fortunately, the tiff format is very flexible and allows us to easily store the part of the metadata that doesn’t change over time in a dedicated block of the file. However, this metadata block is only accessible if you know where to look. The tiff files can still be read by any reader conforming to the baseline tiff specification, but those readers will not be aware of the ScanImage metadata block. The ScanImageTiffReader knows how to extract this metadata and also provides fast access to the images themselves.

ScanImage tiff files store the following kinds of data:

Kind Description
data The image data itself
metadata Frame-invariant metadata such as the microscope configuration and region of interest descriptions.
descriptions Frame-varying metadata such as timestamps and I2C data.

The metadata sections themselves are encoded as either matlab evaluable strings or json.

The ScanImage documentation has a very detailed description of how data is stored in a ScanImage Tiff.

Python Interface


The ScanImageTiffReader class reads Tiff’s produced by ScanImage. The reader exposes functionality for extracting the image stack into a numpy array, extracting the image description from individual frames, and for accessing other ScanImage specific metadata stored in the file. Additionally, we put some effort into making the reader fast.

You can also use this reader for Tiff’s and BigTiffs that weren’t produced by ScanImage, but some might not work. In particular, it lacks support for compression and tiling. Let us know if you have a Tiff you’d particularly like to read.


>>> reader=ScanImageTiffReader("data/TR_003.tif")
>>> reader.close()

The reader supports python’s with syntax:

>>> import ScanImageTiffReader
>>> with ScanImageTiffReader.ScanImageTiffReader('data/TR_003.tif') as reader:
...     print(reader.shape())
[10, 512, 512]

Notice, that in numpy dimensions are ordered like: [z,y,x].

Just in case it’s useful, the API version used by the library can be queried by running:

>>> print(ScanImageTiffReader.api_version()) #doctest: +ELLIPSIS
Version ...


class ScanImageTiffReader.ScanImageTiffReader(filename)[source]

Reader for ScanImage Tiff and BigTiff files.

The constructor opens a file and builds an index by scanning through the file.

static api_version()[source]

Returns a string stating the version of the c library that this python code is using.

>>> print(ScanImageTiffReader.api_version()) #doctest: +ELLIPSIS
Version ...

Closes the open file context releasing any bound resources.


Returns a numpy array with the image stack.

Return type:numpy.array
>>> v=ScanImageTiffReader("data/TR_003.tif").data()
>>> print(v.shape)
(10, 512, 512)
>>> print(v[0][0][0:10])
[6 6 7 5 7 8 8 9 6 9]

Notice, that in numpy dimensions are ordered like: [z,y,x].


Returns the contents of the image description tag for the indicated frame.

Parameters:iframe – An integer between 0 and len(self)-1
Return type:string
>>> with ScanImageTiffReader("data/res_00001.tif") as reader:
...     print(reader.description(0))
frameNumbers = 1
acquisitionNumbers = 1
frameNumberAcquisition = 1
frameTimestamps_sec = 0.000000000
acqTriggerTimestamps_sec = 0.000000000
nextFileMarkerTimestamps_sec = -1.000000000
endOfAcquisition = 0
endOfAcquisitionMode = 0
dcOverVoltage = 0
epoch = [1601  1  1  0  0 25.045]
auxTrigger0 = []
auxTrigger1 = []
auxTrigger2 = []
auxTrigger3 = []
I2CData = {}

Another example from a file that was saved using the json-style header:

>>> with ScanImageTiffReader("data/resj_2018a_00002.tif") as reader:
...     print(reader.description(0)) # doctest: +NORMALIZE_WHITESPACE
  "frameNumbers": 1,
  "acquisitionNumbers": 1,
  "frameNumberAcquisition": 1,
  "frameTimestamps_sec": 0.000000000,
  "acqTriggerTimestamps_sec": -0.000087000,
  "nextFileMarkerTimestamps_sec": -1.000000000,
  "endOfAcquisition": 0,
  "endOfAcquisitionMode": 0,
  "dcOverVoltage": 0,
  "epoch": [2018,10, 9,11,58,49.866],
  "auxTrigger0": [],
  "auxTrigger1": [],
  "auxTrigger2": [],
  "auxTrigger3": [],
  "I2CData": []

Returns a tuple corresponding to

Return type:numpy.dtype
>>> with ScanImageTiffReader("data/resj_00001.tif") as reader:
...     print(reader.dtype())

Reads the ScanImage metadata section from the file. This data section is not part of the Tiff specification, so common Tiff readers will not be able to access this data.

In ScanImage 2016, this is a JSON string. For previous versions of ScanImage, this is a bytestring that must be deserialized in matlab.

Return type:string
>>> import json
>>> with ScanImageTiffReader("data/resj_00001.tif") as reader:
...     o=json.loads(reader.metadata())
...     print(o["RoiGroups"]["imagingRoiGroup"]["rois"]["scanfields"]["affine"])
[[23.4, 0, -11.7], [0, 23.4, -11.7], [0, 0, 1]]

Opens a ScanImage tiff file for reading.

Called by the constructor. Normally you don’t need to use this function. It’s possible, however to reuse an instance of this class:

>>> reader=ScanImageTiffReader("data/TR_003.tif")
>>> reader.close()
>>> print(reader.shape())
[10, 512, 512]
>>> reader.close()

However, there’s probably no real reason to use this pattern over just constructing new readers.


Returns a tuple corresponding to

>>> with ScanImageTiffReader("data/resj_00001.tif") as reader:
...     print(reader.shape())
[10, 512, 512]


Version Highlights
  • Support for loading a selected interval of frames
  • Python 3.6 support.
  • Made pip installable.
  • Fix: Properly return error messages to caller.
  • Initial release


The ScanImageTiffReader is fast.

The ScanImageTiffReader was designed to be fast. When done right, reading a Tiff file can have very low overhead. One should expect that the read performance is roughly the same as the bandwidth-limiting bottleneck, usually the hard-drive used for storage.

Thanks to solid-state storage and aggressive caching of files by the operating systems, read speeds of 500 MB/s or greater are very acheivable. Realizing this bandwidth improvement can reduce read times by an order of magnitude or more.

Reading from an SSD drive (Samsung 840 Pro),


Benchmark speeds for the storage drive used here.

A roughly 6GB file should take ~11.4 seconds to read. Using the ScanImageTiffReader:


Time to read a stack using the ScanImageTiffReader API in Julia. The choice of language doesn’t make a significant difference.

We can compute the effective read bandwidth by dividing the total byte size of the file (6.277 GB) by the amount of time it took to read the file. In the Julia example above, this comes out to 430 MB/s.

Due to caching by the operating system, we sometimes exceed the expected speed of the hard drive. The behavior of this kind of file-system caching might vary between operating systems.