# Reading and Writing for Images and Transforms¶

## Images¶

There are numerous file formats support by SimpleITK’s image readers and writers. Support for a particular format is handled by a specific ITK ImageIO class. By default, the ImageIO is automatically determined for a particular file based on the file name suffix and/or the contents of the file’s header. Advanced SimpleITK installations can configure or extend which file formats are supported by SimpleITK. A list of registered ImageIO’s can be found using the GetRegisteredImageIOs() method, but is posted here:

  ImageFileReader reader = new ImageFileReader();

ImageFileWriter writer = new ImageFileWriter();
writer.SetFileName(outputImageFileName);
writer.Execute(image);


The above example specifies using the PNGImageIO to read the file. If that line is omitted, SimpleITK would determine which IO to use automatically, based on the file name’s suffix and/or the file’s header.

A more compact example using SimpleITK’s procedural interface:

  Image image = sitk.ReadImage(inputImageFileName, PixelIDValueEnum.sitkUnknown, "PNGImageIO");
sitk.WriteImage(image, outputImageFileName);


Similarly, if the imageIO parameter is omitted, SimpleITK will determine which IO to use automatically.

## Transformations¶

In SimpleITK, transformation files can be written in several different formats. Just as there are numerous IOs for images, there are several for transforms, including TxtTransformIO, MINCTransformIO, HDF5TransformIO, and MatlabTransformIO (although this list can be extended as well). These support a variety of file formats, including .txt, .tfm, .xfm, .hdf and .mat.

Because of the size of displacement fields, writing them may require more careful attention. To save a displacement field we recommend using one of the binary transformation file formats (e.g. .hdf, .mat). Saving it in a text based format results in significantly larger files and longer IO runtimes. Another option is to save the displacement field found in a DisplacementFieldTransform object as an image (.nrrd, .nhdr, .mha, .mhd, .nii, .nii.gz).

Here is a simple example of creating a transformation, writing it to a file, reading it back, and then comparing the results.

  Euler2DTransform basic_transform = new Euler2DTransform();
VectorDouble trans = new VectorDouble( new double[] {2.0, 3.0} );
basic_transform.SetTranslation(trans);

sitk.WriteTransform(basic_transform, "euler2D.tfm");


In all languages, except Python, read_result returns an object of the generic sitk.Transform() class and basic_transform creates a sitk.Euler2DTransform() object, but both represent the same transformation. Although this example only uses a single SimpleITK transformation, a .tfm file can hold a composite (set of transformations).