Input/Output module

Image I/O

This module allows a management of inputs and outputs (2D/3D images and metadata) through the functions imread and imsave. Supported formats are: [‘.tif’, ‘.tiff’, ‘.mha’, ‘.mha.gz’, ‘.inr’, ‘.inr.gz’, ‘.inr.zip’].

timagetk.io.io_image.imread(fname)[source]

Read an image (2D/3D).

The supported formats are defined in POSS_EXT.

Parameters:fname (str) – path to the image
Returns:sp_image – image and metadata (such as voxelsize, extent, type, etc.)
Return type:SpatialImage

Example

>>> from timagetk.util import data_path
>>> from timagetk.io import imread
>>> from timagetk.components import SpatialImage
>>> image_path = data_path('filtering_src.inr')
>>> sp_image = imread(image_path)
>>> isinstance(sp_image, SpatialImage)
True
timagetk.io.io_image.imsave(fname, sp_img)[source]

Save an image (2D/3D).

The supported formats are defined in POSS_EXT.

Parameters:
  • fname (str) – save path
  • sp_img (SpatialImage) – SpatialImage instance

Example

>>> from timagetk.io import imsave
>>> from timagetk.util import data_path
>>> from timagetk.components import SpatialImage
>>> test_array = np.ones((5,5), dtype=np.uint8)
>>> sp_image = SpatialImage(test_array)
>>> save_path = data_path('test_output.tif')
>>> imsave(save_path, sp_image)
timagetk.io.io_image.apply_mask(img, mask_filename, masking_value=0, crop_mask=False)[source]

Load and apply a z-projection mask (2D) to a SpatialImage (2D/3D). In case of an intensity image, allows to remove unwanted signal intensities. If the SpatialImage is 3D, it is applied in z-direction. The mask should contain a distinct “masking value”.

Parameters:
  • img (SpatialImage) – SpatialImage to modify with the mask
  • mask_filename (str) – string giving the location of the mask file
  • masking_value (int, optional) – value (default 0) defining the masked region
  • crop_mask (bool, optional) – if True (default False), the returned SpatialImage is cropped around the non-masked region
Returns:

masked_image – the masked SpatialImage.

Return type:

SpatialImage

Accepted image formats are: timagetk.io.io_image.POSS_EXT.

Transformation I/O

Input/Output of transformations.

timagetk.io.io_trsf.read_trsf(filename)[source]

Read a transformation file.

Parameters:filename (str) – name of the file
Returns:tsrf – loaded BalTrsf object
Return type:BalTrsf
timagetk.io.io_trsf.save_trsf(trsf, filename, compress=False)[source]

Write a BalTrsf trsf under given filename.

Parameters:
  • trsf (BalTrsf) – BalTrsf object to save
  • filename (str) – name of the file to write
  • compress (bool, optional) – if True (default False) the saved transformation matrix is compressed

Notes

Beware that ‘vx’, ‘vy’ & ‘vz’ are fields for both voxelsize and non-linear deformation!