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’, ‘’].[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


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

Save an image (2D/3D).

The supported formats are defined in POSS_EXT.

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


>>> from 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), 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”.

  • 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

masked_image – the masked SpatialImage.

Return type:


Accepted image formats are:

Transformation I/O

Input/Output of transformations.[source]

Read a transformation file.

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

Write a BalTrsf trsf under given filename.

  • 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


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