Module: external.tifffile
¶
skimage.external.tifffile.imsave (file, data, …) |
Write image data to TIFF file. |
skimage.external.tifffile.imread (files, **kwargs) |
Return image data from TIFF file(s) as numpy array. |
skimage.external.tifffile.imshow (data[, …]) |
Plot n-dimensional images using matplotlib.pyplot. |
skimage.external.tifffile.parse_kwargs (…) |
Return dict with keys from keys|keyvals and values from kwargs|keyvals. |
skimage.external.tifffile.TiffFile (arg[, …]) |
Read image and metadata from TIFF, STK, LSM, and FluoView files. |
skimage.external.tifffile.TiffWriter (file[, …]) |
Write image data to TIFF file. |
skimage.external.tifffile.TiffSequence (files) |
Sequence of image files. |
imsave¶
-
skimage.external.tifffile.
imsave
(file, data, **kwargs)[source]¶ Write image data to TIFF file.
Refer to the TiffWriter class and member functions for documentation.
Parameters: file : str or binary stream
File name or writable binary stream, such as a open file or BytesIO.
data : array_like
Input image. The last dimensions are assumed to be image depth, height, width, and samples.
kwargs : dict
Parameters ‘append’, ‘byteorder’, ‘bigtiff’, ‘software’, and ‘imagej’, are passed to the TiffWriter class. Parameters ‘photometric’, ‘planarconfig’, ‘resolution’, ‘compress’, ‘colormap’, ‘tile’, ‘description’, ‘datetime’, ‘metadata’, ‘contiguous’ and ‘extratags’ are passed to the TiffWriter.save function.
Examples
>>> data = numpy.random.rand(2, 5, 3, 301, 219) >>> imsave('temp.tif', data, compress=6, metadata={'axes': 'TZCYX'})
imread¶
-
skimage.external.tifffile.
imread
(files, **kwargs)[source]¶ Return image data from TIFF file(s) as numpy array.
Refer to the TiffFile class and member functions for documentation.
Parameters: files : str, binary stream, or sequence
File name, seekable binary stream, glob pattern, or sequence of file names.
kwargs : dict
Parameters ‘multifile’, ‘multifile_close’, ‘pages’, ‘fastij’, and ‘is_ome’ are passed to the TiffFile class. The ‘pattern’ parameter is passed to the TiffSequence class. Other parameters are passed to the asarray functions. The first image series is returned if no arguments are provided.
Examples
>>> imsave('temp.tif', numpy.random.rand(3, 4, 301, 219)) >>> im = imread('temp.tif', key=0) >>> im.shape (4, 301, 219) >>> ims = imread(['temp.tif', 'temp.tif']) >>> ims.shape (2, 3, 4, 301, 219)
imshow¶
-
skimage.external.tifffile.
imshow
(data, title=None, vmin=0, vmax=None, cmap=None, bitspersample=None, photometric='rgb', interpolation=None, 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 width and length.
kwargs : optional
Arguments for matplotlib.pyplot.imshow.
parse_kwargs¶
-
skimage.external.tifffile.
parse_kwargs
(kwargs, *keys, **keyvalues)[source]¶ Return dict with keys from keys|keyvals and values from kwargs|keyvals.
Existing keys are deleted from kwargs.
>>> kwargs = {'one': 1, 'two': 2, 'four': 4} >>> kwargs2 = parse_kwargs(kwargs, 'two', 'three', four=None, five=5) >>> kwargs == {'one': 1} True >>> kwargs2 == {'two': 2, 'four': 4, 'five': 5} True
TiffFile
¶
-
class
skimage.external.tifffile.
TiffFile
(arg, name=None, offset=None, size=None, multifile=True, multifile_close=True, pages=None, fastij=True, is_ome=None)[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’ context manager.
Examples
>>> with TiffFile('temp.tif') as tif: ... data = tif.asarray() ... data.shape (5, 301, 219)
Attributes
pages (list of TiffPage) All TIFF pages in file. series (list of TiffPageSeries) 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, pages=None, fastij=True, is_ome=None)[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.
pages : sequence of int
Indices of the pages to read. If None (default) all pages are read. Can be used to read only the first page with pages=[0]. Specifying pages might invalidate series based on metadata.
fastij : bool
If True (default), try to use only the metadata from the first page of ImageJ files. Significantly speeds up loading movies with thousands of pages.
is_ome : bool
If False, disable processing of OME-XML metadata.
-
asarray
(key=None, series=None, memmap=False, tempdir=None)[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 or TiffPageSeries
Defines which series of pages to return as array.
memmap : bool
If True, return an read-only array stored in a binary file on disk if possible. The TIFF file is used if possible, else a temporary file is created.
tempdir : str
The directory where the memory-mapped file will be created.
-
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_fei
¶ 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_indexed
¶ 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_rgb
¶ Lazy object attribute whose value is computed on first access.
-
is_scn
¶ Lazy object attribute whose value is computed on first access.
-
is_sem
¶ Lazy object attribute whose value is computed on first access.
-
is_stk
¶ Lazy object attribute whose value is computed on first access.
-
is_tvips
¶ Lazy object attribute whose value is computed on first access.
-
is_vista
¶ Lazy object attribute whose value is computed on first access.
-
series
¶ Lazy object attribute whose value is computed on first access.
-
TiffWriter
¶
-
class
skimage.external.tifffile.
TiffWriter
(file, append=False, bigtiff=False, byteorder=None, software='tifffile.py', imagej=False)[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’ context manager.
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__
(file, append=False, bigtiff=False, byteorder=None, software='tifffile.py', imagej=False)[source]¶ Open a TIFF file for writing.
Existing files are overwritten by default. Use bigtiff=True when creating files larger than 2 GB.
Parameters: file : str, binary stream, or FileHandle
File name or writable binary stream, such as a open file or BytesIO. The file is created if it does not exist.
append : bool
If True and ‘file’ is an existing standard TIFF file, image data and tags are appended to the file. Appending data may corrupt specifically formatted TIFF files such as LSM, STK, ImageJ, NIH, or FluoView.
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 file. Saved with the first page in the file only.
imagej : bool
If True, write an ImageJ hyperstack compatible file. This format can handle data types uint8, uint16, or float32 and data shapes up to 6 dimensions in TZCYXS order. RGB images (S=3 or S=4) must be uint8. ImageJ’s default byte order is big endian but this implementation uses the system’s native byte order by default. ImageJ does not support BigTIFF format or LZMA compression. The ImageJ file format is undocumented.
-
TAGS
= {'bits_per_sample': 258, 'color_map': 320, 'compression': 259, 'datetime': 306, 'document_name': 269, 'extra_samples': 338, 'image_depth': 32997, 'image_description': 270, 'image_length': 257, 'image_width': 256, 'new_subfile_type': 254, 'orientation': 274, 'page_name': 285, 'photometric': 262, 'planar_configuration': 284, 'predictor': 317, 'resolution_unit': 296, 'rows_per_strip': 278, 'sample_format': 339, 'samples_per_pixel': 277, 'smax_sample_value': 341, 'smin_sample_value': 340, 'software': 305, 'strip_byte_counts': 279, 'strip_offsets': 273, 'subfile_type': 255, 'tile_byte_counts': 325, 'tile_depth': 32998, 'tile_length': 323, 'tile_offsets': 324, 'tile_width': 322, 'x_resolution': 282, 'y_resolution': 283}¶
-
TYPES
= {'2I': 5, '2i': 10, 'B': 1, 'H': 3, 'I': 4, 'Q': 16, 'b': 6, 'd': 12, 'f': 11, 'h': 8, 'i': 9, 'q': 17, 's': 2}¶
-
save
(data, photometric=None, planarconfig=None, tile=None, contiguous=True, compress=0, colormap=None, description=None, datetime=None, resolution=None, metadata={}, extratags=())[source]¶ Write image data and tags to TIFF file.
Image data are written in one stripe per plane by default. 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’ tags are derived from the data type.
Parameters: data : numpy.ndarray
Input image. The last dimensions are assumed to be image depth, height (length), width, and samples. If a colormap is provided, the dtype must be uint8 or uint16 and the data values are indices into the last dimension of the colormap.
photometric : {‘minisblack’, ‘miniswhite’, ‘rgb’, ‘palette’, ‘cfa’}
The color space of the image data. By default this setting is inferred from the data shape and the value of colormap. For CFA images, DNG tags must be specified in extratags.
planarconfig : {‘contig’, ‘planar’}
Specifies if samples are stored contiguous or in separate planes. By default this setting is inferred from the data shape. If this parameter is set, extra samples are used to store grayscale images. ‘contig’: last dimension contains samples. ‘planar’: third last dimension contains samples.
tile : tuple of int
The shape (depth, length, width) of image tiles to write. If None (default), image data are written in one stripe per plane. The tile length and width must be a multiple of 16. If the tile depth is provided, the SGI image_depth and tile_depth tags are used to save volume data. Few software can read the SGI format, e.g. MeVisLab.
contiguous : bool
If True (default) and the data and parameters are compatible with previous ones, if any, the data are stored contiguously after the previous one. Parameters ‘photometric’ and ‘planarconfig’ are ignored.
compress : int or ‘lzma’
Values from 0 to 9 controlling the level of zlib compression. If 0, data are written uncompressed (default). Compression cannot be used to write contiguous files. If ‘lzma’, LZMA compression is used, which is not available on all platforms.
colormap : numpy.ndarray
RGB color values for the corresponding data value. Must be of shape (3, 2**(data.itemsize*8)) and dtype uint16.
description : str
The subject of the image. Saved with the first page only. Cannot be used with the ImageJ format.
datetime : datetime
Date and time of image creation. Saved with the first page only. If None (default), the current date and time is used.
resolution : (float, float[, str]) or ((int, int), (int, int)[, str])
X and Y resolutions in pixels per resolution unit as float or rational numbers. A third, optional parameter specifies the resolution unit, which must be None (default for ImageJ), ‘inch’ (default), or ‘cm’.
metadata : dict
Additional meta data to be saved along with shape information in JSON or ImageJ formats in an image_description tag. If None, do not write a second image_description tag.
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, 2i, 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.
-
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.
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. Binary streams are not supported.
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.
-
asarray
(memmap=False, tempdir=None, *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 do not match.
-