# Save itk image with custom voxel spacing

**URL:** https://discourse.itk.org/t/save-itk-image-with-custom-voxel-spacing/3634
**Category:** Beginner Questions
**Tags:** python
**Created:** [November 19, 2020, 1:58pm UTC](https://discourse.itk.org/t/save-itk-image-with-custom-voxel-spacing/3634 "2020-11-19T13:58:11Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![farhan\_009](https://discourse.itk.org/user_avatar/discourse.itk.org/farhan_009/32/1945_2.png) [@farhan\_009](https://discourse.itk.org/u/farhan_009)
#### Post date: [November 19, 2020, 1:58pm UTC](https://discourse.itk.org/t/save-itk-image-with-custom-voxel-spacing/3634/1 "2020-11-19T13:58:11Z")

</div>

Hello everyone!  
I got output from the model which is a NumPy array of shape [128,448,448] .I converted to itkobject using **itk.image\_view\_from\_array(predicted\_label)** and save the image with **itkwrite**.But shape of input image was [448 448 128] with voxel spacing[0.5134 0.5134 0.8] but **itk.write** gives output[448 448 128] with voxel spacing [1 1 1].Any solution for this problem as I want output image with custom voxel spacing as shape([448 448 128],[0.5134 0.5134 0.8])  
I tried to use nibabel but reshape the image distorts the result.

---

<div class="post-metadata">

### Author: ![farhan\_009](https://discourse.itk.org/user_avatar/discourse.itk.org/farhan_009/32/1945_2.png) [@farhan\_009](https://discourse.itk.org/u/farhan_009)
#### Post date: [November 19, 2020, 2:00pm UTC](https://discourse.itk.org/t/save-itk-image-with-custom-voxel-spacing/3634/2 "2020-11-19T14:00:04Z")

</div>

@matt.mccormick, @Stephen_Aylward

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [November 19, 2020, 2:30pm UTC](https://discourse.itk.org/t/save-itk-image-with-custom-voxel-spacing/3634/3 "2020-11-19T14:30:09Z")

</div>

You could use [ChangeInformationImageFilter](https://itk.org/Doxygen/html/classitk_1_1ChangeInformationImageFilter.html). That is especially easy if you have the original image in ITK format. [Example](https://itk.org/ITKExamples/src/Filtering/ImageGrid/ChangeImageOriginSpacingOrDirection/Documentation.html).

---

<div class="post-metadata">

### Author: ![phcerdan](https://discourse.itk.org/user_avatar/discourse.itk.org/phcerdan/32/286_2.png) [@phcerdan](https://discourse.itk.org/u/phcerdan)
#### Post date: [November 19, 2020, 2:33pm UTC](https://discourse.itk.org/t/save-itk-image-with-custom-voxel-spacing/3634/4 "2020-11-19T14:33:42Z")

</div>

Or `SetSpacing` in your itk python image.

---

<div class="post-metadata">

### Author: ![farhan\_009](https://discourse.itk.org/user_avatar/discourse.itk.org/farhan_009/32/1945_2.png) [@farhan\_009](https://discourse.itk.org/u/farhan_009)
#### Post date: [November 19, 2020, 3:29pm UTC](https://discourse.itk.org/t/save-itk-image-with-custom-voxel-spacing/3634/5 "2020-11-19T15:29:20Z")

</div>

> [@dzenanz](#):
>
> [ChangeInformationImageFilter](https://itk.org/Doxygen/html/classitk_1_1ChangeInformationImageFilter.html)

```
    filter = itk.ChangeInformationImageFilter.New(___) # itk image
    filter.SetUseReferenceImage(True)
    filter.SetChangeSpacing(True)
    output_spacing = [0.5134, 0.5134 0.8]
    filter.SetOutputSpacing(output_spacing)
    writerInput =filter.GetOutput()
    writer = itk.ImageFileWriter[ImageType].New()
    writer.SetFileName(outputFilePath)
    writer.SetInput(writerInput)
    writer.Update()

```

is this will work ?

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [November 19, 2020, 3:58pm UTC](https://discourse.itk.org/t/save-itk-image-with-custom-voxel-spacing/3634/6 "2020-11-19T15:58:30Z")

</div>

> [@farhan\_009](#):
>
> `filter.SetUseReferenceImage(True)`

This should be false, as you are not using a reference image. The rest looks good.

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [November 19, 2020, 4:00pm UTC](https://discourse.itk.org/t/save-itk-image-with-custom-voxel-spacing/3634/7 "2020-11-19T16:00:20Z")

</div>

But if you are only looking to change spacing, Pablo’s suggestion is probably simpler.

```auto
output_spacing = [0.5134, 0.5134 0.8]
image.SetSpacing(output_spacing)
itk.imwrite(image, outputFilePath)

```

---

<div class="post-metadata">

### Author: ![farhan\_009](https://discourse.itk.org/user_avatar/discourse.itk.org/farhan_009/32/1945_2.png) [@farhan\_009](https://discourse.itk.org/u/farhan_009)
#### Post date: [November 20, 2020, 3:15am UTC](https://discourse.itk.org/t/save-itk-image-with-custom-voxel-spacing/3634/8 "2020-11-20T03:15:51Z")

</div>

Thank you, sir @dzenanz @phcerdan, it worked.
