Module: external.tifffile

skimage.external.tifffile.imread(files, **kwargs) Return image data from TIFF file(s) as numpy array.
skimage.external.tifffile.imsave(filename, ...) Write image data to TIFF file.
skimage.external.tifffile.imshow(data[, ...]) Plot n-dimensional images using matplotlib.pyplot.
skimage.external.tifffile.TiffFile(arg[, ...]) Read image and metadata from TIFF, STK, LSM, and FluoView files.
skimage.external.tifffile.TiffSequence(files) Sequence of image files.
skimage.external.tifffile.TiffWriter(filename) Write image data to TIFF file.

imread

skimage.external.tifffile.imread(files, **kwargs)[source]

Return image data from TIFF file(s) as numpy array.

The first image series is returned if no arguments are provided.

Parameters:

files : str or list

File name, glob pattern, or list of file names.

key : int, slice, or sequence of page indices

Defines which pages to return as array.

series : int

Defines which series of pages in file to return as array.

multifile : bool

If True (default), OME-TIFF data may include pages from multiple files.

pattern : str

Regular expression pattern that matches axes names and indices in file names.

kwargs : dict

Additional parameters passed to the TiffFile or TiffSequence asarray function.

Examples

>>> im = imread('test.tif', key=0)  
>>> im.shape  
(256, 256, 4)
>>> ims = imread(['test.tif', 'test.tif'])  
>>> ims.shape  
(2, 256, 256, 4)

imsave

skimage.external.tifffile.imsave(filename, data, **kwargs)[source]

Write image data to TIFF file.

Refer to the TiffWriter class and member functions for documentation.

Parameters:

filename : str

Name of file to write.

data : array_like

Input image. The last dimensions are assumed to be image depth, height, width, and samples.

kwargs : dict

Parameters ‘byteorder’, ‘bigtiff’, and ‘software’ are passed to the TiffWriter class. Parameters ‘photometric’, ‘planarconfig’, ‘resolution’, ‘description’, ‘compress’, ‘volume’, and ‘extratags’ are passed to the TiffWriter.save function.

Examples

>>> data = numpy.random.rand(2, 5, 3, 301, 219)
>>> description = u'{"shape": %s}' % str(list(data.shape))  
>>> imsave('temp.tif', data, compress=6,  
...        extratags=[(270, 's', 0, description, True)])

imshow

skimage.external.tifffile.imshow(data, title=None, vmin=0, vmax=None, cmap=None, bitspersample=None, photometric='rgb', interpolation='nearest', dpi=96, figure=None, subplot=111, maxdim=8192, **kwargs)[source]

Plot n-dimensional images using matplotlib.pyplot.

Return figure, subplot and plot axis. Requires pyplot already imported from matplotlib import pyplot.

Parameters:

bitspersample : int or None

Number of bits per channel in integer RGB images.

photometric : {‘miniswhite’, ‘minisblack’, ‘rgb’, or ‘palette’}

The color space of the image data.

title : str

Window and subplot title.

figure : matplotlib.figure.Figure (optional).

Matplotlib to use for plotting.

subplot : int

A matplotlib.pyplot.subplot axis.

maxdim : int

maximum image size in any dimension.

kwargs : optional

Arguments for matplotlib.pyplot.imshow.

TiffFile

class skimage.external.tifffile.TiffFile(arg, name=None, offset=None, size=None, multifile=True, multifile_close=True)[source]

Bases: object

Read image and metadata from TIFF, STK, LSM, and FluoView files.

TiffFile instances must be closed using the close method, which is automatically called when using the ‘with’ statement.

Examples

>>> with TiffFile('test.tif') as tif:  
...     data = tif.asarray()
...     data.shape
(256, 256, 4)

Attributes

pages (list) All TIFF pages in file.
series (list of Records(shape, dtype, axes, TiffPages)) TIFF pages with compatible shapes and types.
micromanager_metadata: dict Extra MicroManager non-TIFF metadata in the file, if exists.
All attributes are read-only.  
__init__(arg, name=None, offset=None, size=None, multifile=True, multifile_close=True)[source]

Initialize instance from file.

Parameters:

arg : str or open file

Name of file or open file object. The file objects are closed in TiffFile.close().

name : str

Optional name of file in case ‘arg’ is a file handle.

offset : int

Optional start position of embedded file. By default this is the current file position.

size : int

Optional size of embedded file. By default this is the number of bytes from the ‘offset’ to the end of the file.

multifile : bool

If True (default), series may include pages from multiple files. Currently applies to OME-TIFF only.

multifile_close : bool

If True (default), keep the handles of other files in multifile series closed. This is inefficient when few files refer to many pages. If False, the C runtime may run out of resources.

asarray(key=None, series=None, memmap=False)[source]

Return image data from multiple TIFF pages as numpy array.

By default the first image series is returned.

Parameters:

key : int, slice, or sequence of page indices

Defines which pages to return as array.

series : int

Defines which series of pages to return as array.

memmap : bool

If True, return an array stored in a binary file on disk if possible.

close()[source]

Close open file handle(s).

filehandle

Return file handle.

filename

Return name of file handle.

fstat

Lazy object attribute whose value is computed on first access.

is_bigtiff

Lazy object attribute whose value is computed on first access.

is_fluoview

Lazy object attribute whose value is computed on first access.

is_imagej

Lazy object attribute whose value is computed on first access.

is_lsm

Lazy object attribute whose value is computed on first access.

is_mdgel

Lazy object attribute whose value is computed on first access.

is_mediacy

Lazy object attribute whose value is computed on first access.

is_micromanager

Lazy object attribute whose value is computed on first access.

is_nih

Lazy object attribute whose value is computed on first access.

is_ome

Lazy object attribute whose value is computed on first access.

is_palette

Lazy object attribute whose value is computed on first access.

is_rgb

Lazy object attribute whose value is computed on first access.

is_stk

Lazy object attribute whose value is computed on first access.

series

Lazy object attribute whose value is computed on first access.

TiffSequence

class skimage.external.tifffile.TiffSequence(files, imread=<class 'skimage.external.tifffile.tifffile.TiffFile'>, pattern='axes', *args, **kwargs)[source]

Bases: object

Sequence of image files.

The data shape and dtype of all files must match.

Examples

>>> tifs = TiffSequence("test.oif.files/*.tif")  
>>> tifs.shape, tifs.axes  
((2, 100), 'CT')
>>> data = tifs.asarray()  
>>> data.shape  
(2, 100, 256, 256)

Attributes

files (list) List of file names.
shape (tuple) Shape of image sequence.
axes (str) Labels of axes in shape.
__init__(files, imread=<class 'skimage.external.tifffile.tifffile.TiffFile'>, pattern='axes', *args, **kwargs)[source]

Initialize instance from multiple files.

Parameters:

files : str, or sequence of str

Glob pattern or sequence of file names.

imread : function or class

Image read function or class with asarray function returning numpy array from single file.

pattern : str

Regular expression pattern that matches axes names and sequence indices in file names. By default this matches Olympus OIF and Leica TIFF series.

exception ParseError[source]

Bases: exceptions.Exception

TiffSequence.asarray(memmap=False, *args, **kwargs)[source]

Read image data from all files and return as single numpy array.

If memmap is True, return an array stored in a binary file on disk. The args and kwargs parameters are passed to the imread function.

Raise IndexError or ValueError if image shapes don’t match.

TiffSequence.close()[source]

TiffWriter

class skimage.external.tifffile.TiffWriter(filename, bigtiff=False, byteorder=None, software='tifffile.py')[source]

Bases: object

Write image data to TIFF file.

TiffWriter instances must be closed using the close method, which is automatically called when using the ‘with’ statement.

Examples

>>> data = numpy.random.rand(2, 5, 3, 301, 219)
>>> with TiffWriter('temp.tif', bigtiff=True) as tif:
...     for i in range(data.shape[0]):
...         tif.save(data[i], compress=6)
__init__(filename, bigtiff=False, byteorder=None, software='tifffile.py')[source]

Create a new TIFF file for writing.

Use bigtiff=True when creating files greater than 2 GB.

Parameters:

filename : str

Name of file to write.

bigtiff : bool

If True, the BigTIFF format is used.

byteorder : {‘<’, ‘>’}

The endianness of the data in the file. By default this is the system’s native byte order.

software : str

Name of the software used to create the image. Saved with the first page only.

TAGS = {'strip_byte_counts': 279, 'strip_offsets': 273, 'resolution_unit': 296, 'x_resolution': 282, 'photometric': 262, 'y_resolution': 283, 'subfile_type': 255, 'tile_byte_counts': 325, 'tile_offsets': 324, 'tile_width': 322, 'image_depth': 32997, 'bits_per_sample': 258, 'tile_depth': 32998, 'image_length': 257, 'datetime': 306, 'document_name': 269, 'orientation': 274, 'planar_configuration': 284, 'sample_format': 339, 'compression': 259, 'image_description': 270, 'fill_order': 266, 'image_width': 256, 'rows_per_strip': 278, 'color_map': 320, 'page_name': 285, 'samples_per_pixel': 277, 'new_subfile_type': 254, 'tile_length': 323, 'extra_samples': 338, 'predictor': 317, 'software': 305}
TYPES = {'B': 1, 'I': 4, 'H': 3, 'Q': 16, '2I': 5, 'b': 6, 'd': 12, 'f': 11, 'i': 9, 'h': 8, 'q': 17, 's': 2}
close()[source]
save(data, photometric=None, planarconfig=None, resolution=None, description=None, volume=False, writeshape=False, compress=0, extratags=())[source]

Write image data to TIFF file.

Image data are written in one stripe per plane. Dimensions larger than 2 to 4 (depending on photometric mode, planar configuration, and SGI mode) are flattened and saved as separate pages. The ‘sample_format’ and ‘bits_per_sample’ TIFF tags are derived from the data type.

Parameters:

data : array_like

Input image. The last dimensions are assumed to be image depth, height, width, and samples.

photometric : {‘minisblack’, ‘miniswhite’, ‘rgb’}

The color space of the image data. By default this setting is inferred from the data shape.

planarconfig : {‘contig’, ‘planar’}

Specifies if samples are stored contiguous or in separate planes. By default this setting is inferred from the data shape. ‘contig’: last dimension contains samples. ‘planar’: third last dimension contains samples.

resolution : (float, float) or ((int, int), (int, int))

X and Y resolution in dots per inch as float or rational numbers.

description : str

The subject of the image. Saved with the first page only.

compress : int

Values from 0 to 9 controlling the level of zlib compression. If 0, data are written uncompressed (default).

volume : bool

If True, volume data are stored in one tile (if applicable) using the SGI image_depth and tile_depth tags. Image width and depth must be multiple of 16. Few software can read this format, e.g. MeVisLab.

writeshape : bool

If True, write the data shape to the image_description tag if necessary and no other description is given.

extratags: sequence of tuples

Additional tags as [(code, dtype, count, value, writeonce)].

code
: int

The TIFF tag Id.

dtype
: str

Data type of items in ‘value’ in Python struct format. One of B, s, H, I, 2I, b, h, i, f, d, Q, or q.

count
: int

Number of data values. Not used for string values.

value
: sequence

‘Count’ values compatible with ‘dtype’.

writeonce
: bool

If True, the tag is written to the first page only.