# How to reshape 4D .nii image

**URL:** <https://discourse.itk.org/t/how-to-reshape-4d-nii-image/6655>\
**Category:** Uncategorized\
**Tags:** simpleitk, python\
**Created:** [May 23, 2024, 2:06pm UTC](https://discourse.itk.org/t/how-to-reshape-4d-nii-image/6655 "2024-05-23T14:06:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![debapriya](https://discourse.itk.org/letter_avatar_proxy/v4/letter/d/bc79bd/32.png) [@debapriya](https://discourse.itk.org/u/debapriya)\
**Post date:** [May 23, 2024, 2:06pm UTC](https://discourse.itk.org/t/how-to-reshape-4d-nii-image/6655/1 "2024-05-23T14:06:00Z")

</div>

I have a `.nii` image of size `30, 28, 256, 256`. I want to reshape it to `256, 256, 28, 30`. I tried converting to numpy array and then using `sitk.GetImageFromArray()`. However, the size of the resulting image is `256, 256, 28`. The fourth dimension is lost.

How can I get the desired shape of the image?

---

<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:** [May 23, 2024, 2:16pm UTC](https://discourse.itk.org/t/how-to-reshape-4d-nii-image/6655/2 "2024-05-23T14:16:24Z")

</div>

I believe the PermuteImageFilter can do what you like:  
[https://simpleitk.org/doxygen/latest/html/classitk\_1\_1simple\_1\_1PermuteAxesImageFilter.html](https://simpleitk.org/doxygen/latest/html/classitk_1_1simple_1_1PermuteAxesImageFilter.html)

Also keep in mind that the index order of a numpy array and a SimpleITK image are opposite, so mixing the two with out clear boundaries ( like functions ) is error prone.

---

<div class="post-metadata">

**Author:** ![debapriya](https://discourse.itk.org/letter_avatar_proxy/v4/letter/d/bc79bd/32.png) [@debapriya](https://discourse.itk.org/u/debapriya)\
**Post date:** [May 23, 2024, 2:54pm UTC](https://discourse.itk.org/t/how-to-reshape-4d-nii-image/6655/3 "2024-05-23T14:54:56Z")

</div>

Please tell me the python code to call `PermuteAxesImageFilter`.  
I wrote the following lines,

```auto
image=sitk.ReadImage("Moving.nii",sitk.sitkFloat32)
sitk.sitkPermuteAxesImageFilter.SetOrder(image, [256, 256, 28, 30])

```

It showed the following error,

```auto
AttributeError Traceback (most recent call last)
Cell In[3], line 2
      1 image=sitk.ReadImage("Moving.nii",sitk.sitkFloat32)
----> 2 sitk.sitkPermuteAxesImageFilter.SetOrder(image, [256, 256, 28, 30])

AttributeError: module 'SimpleITK' has no attribute 'sitkPermuteAxesImageFilter'

```

---

<div class="post-metadata">

**Author:** ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)\
**Post date:** [May 23, 2024, 3:03pm UTC](https://discourse.itk.org/t/how-to-reshape-4d-nii-image/6655/4 "2024-05-23T15:03:26Z")

</div>

Hello @debapriya,

Please read [the documentation](https://simpleitk.org/doxygen/latest/html/classitk_1_1simple_1_1PermuteAxesImageFilter.html) @blowekamp pointed to. That is a class, you create an instance and call the class methods as you do in regular Python programming.

---

<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:** [May 23, 2024, 3:22pm UTC](https://discourse.itk.org/t/how-to-reshape-4d-nii-image/6655/5 "2024-05-23T15:22:50Z")

</div>

The order is the axis index [0-4], not the axis size. Something like the following should work:

```auto
import SimpleITK as sitk
img = sitk.Image([30,28,256,256], sitk.sitkFloat32)
sitk.WriteImage(img, "test.nii")
image = sitk.ReadImage("test.nii")
print(f"{image.GetSize()=}")
out = sitk.PermuteAxes(image, order=[3,2,1,0])
print(f"{out.GetSize()=}")

```
