large size when saving image using SimpleITK WriteImage

Dear all,

when I save image using SimpleITK the file size are larger than the one are saved using in 3D Slicer e.g.

imoprt SimpleITK as sitk
img = sitk.ReadImage("imageFilename.nii.gz")
img = sitk.Cast(img, sitk.sitkInt16)

seg = sitk.ReadImage("imageFilename-label.nii.gz")
seg = sitk.Cast(seg, sitk.sitkUInt8)

“imageFilename.nii.gz” size 64MB
“imageFilename-label.nii.gz” size 2.4MB

Slicer output:

“imageFilename.nrrd” 64MB
“imageFilename-label.nrrd” 199.6KB

SimpleITK output:

“imageFilename.nrrd” 112.2MB
“imageFilename-label.nrrd” 56MB

How can I make the size smaller using SimpleITK e.g. to the output of Slicer?



3D Slicer is enabling compression, which is especially effective for label image. If you run help(site.WriteImage) you can see how to enable compression with the useCompression flag:

WriteImage(image: 'Image', fileName: Union[str, List[str]], useCompression: bool = False, compressionLevel=-1, *, imageIO: str = '', compressor: str = '') -> 'None'
    WriteImage is a procedural interface to the ImageFileWriter and ImageSeriesWriter classes which is convenient for
    many image writing tasks.
    For an input image of N dimensions, a series of N-1 dimensional (slices) images can be written by providing a list
    if file names equal to the number of slices in the input image.
     the input image to be written
     a single or a list of file names to be written
     request to compress the written file
     a hint for the amount of compression to be applied during writing
     the name of the ImageIO to perform the writing
     a hint for the compression algorithm to use

You can also further control the compression with compressionLevel and compressor. More details can be found in the sitk.ImageFilterWriter class.

Also please run help(sitk.ImageFileReader) to learn about the outputPixelType option, so that you don’t need to do the extra step with sitk.Cast.


The answer above was helpful, here is what I used based on that:

sitk.WriteImage(seg,"imageFilename-label.nrrd" ,useCompression=True)