# large size when saving image using SimpleITK WriteImage

**URL:** https://discourse.itk.org/t/large-size-when-saving-image-using-simpleitk-writeimage/3509
**Category:** Beginner Questions
**Created:** [October 3, 2020, 12:19pm UTC](https://discourse.itk.org/t/large-size-when-saving-image-using-simpleitk-writeimage/3509 "2020-10-03T12:19:51Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![ibr](https://discourse.itk.org/user_avatar/discourse.itk.org/ibr/32/468_2.png) [@ibr](https://discourse.itk.org/u/ibr)
#### Post date: [October 3, 2020, 12:19pm UTC](https://discourse.itk.org/t/large-size-when-saving-image-using-simpleitk-writeimage/3509/1 "2020-10-03T12:19:51Z")

</div>

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)

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

```

“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?

Thanks

---

<div class="post-metadata">

### Author: ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)
#### Post date: [October 3, 2020, 2:39pm UTC](https://discourse.itk.org/t/large-size-when-saving-image-using-simpleitk-writeimage/3509/2 "2020-10-03T14:39:27Z")

</div>

Hello,

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:

```auto
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.
    
    Parameters
    ----------
    image
     the input image to be written
    fileName
     a single or a list of file names to be written
    useCompression
     request to compress the written file
    compressionLevel
     a hint for the amount of compression to be applied during writing
    imageIO
     the name of the ImageIO to perform the writing
    compressor
     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](https://simpleitk.org/doxygen/latest/html/classitk_1_1simple_1_1ImageFileWriter.html) 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`.

---

<div class="post-metadata">

### Author: ![ibr](https://discourse.itk.org/user_avatar/discourse.itk.org/ibr/32/468_2.png) [@ibr](https://discourse.itk.org/u/ibr)
#### Post date: [April 12, 2021, 3:30pm UTC](https://discourse.itk.org/t/large-size-when-saving-image-using-simpleitk-writeimage/3509/3 "2021-04-12T15:30:28Z")

</div>

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

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

```
