# Convert from 3D numpy array to simple ITK

**URL:** <https://discourse.itk.org/t/convert-from-3d-numpy-array-to-simple-itk/4468>\
**Category:** Beginner Questions\
**Created:** [October 4, 2021, 2:20am UTC](https://discourse.itk.org/t/convert-from-3d-numpy-array-to-simple-itk/4468 "2021-10-04T02:20:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Moha\_data](https://discourse.itk.org/letter_avatar_proxy/v4/letter/m/ecc23a/32.png) [@Moha\_data](https://discourse.itk.org/u/Moha_data)\
**Post date:** [October 4, 2021, 2:20am UTC](https://discourse.itk.org/t/convert-from-3d-numpy-array-to-simple-itk/4468/1 "2021-10-04T02:20:52Z")

</div>

Hello,  
I have a NumPy array (`image_array`) with the size of 400 \* 100 \* 800 (`spacing`: [1, 4, 0.1]).

I used the below code to convert it to `sitk` image for registration:

```auto
image = sitk.GetImageFromArray(image_array)
image.SetOrigin((0, 0, 0))
image.SetSpacing(spacing)

```

When I read the spacing from `sitk` image using `image.GetSpacing()` it is ok but the size (`image.GetSize()`) is different: _800 \* 100 \* 400_

Do I need to reshape the NumPy array before importing it to the `SITK` and then just use the `sitk.GetArrayFromImage` to convert it to the NumPy array and have the same `Spacing` and `Size`?

I tried to inspect the image in`SITK` using `FIJI` but it didn’t show the _spacing_ correctly (all are equal to one)

Thank you

---

<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:** [October 4, 2021, 12:34pm UTC](https://discourse.itk.org/t/convert-from-3d-numpy-array-to-simple-itk/4468/2 "2021-10-04T12:34:54Z")

</div>

Hello @Moha_data,

Welcome to SimpleITK!

SimpleITK and numpy use a different indexing order. SimpleITK uses x-y-z. Numpy uses z-y-x. The `GetImageFromArray` method accounts for this. Thus when you inspect the image size afterwards the reported order is x-y-z which matches your original numpy size. With respect to `SetSpacing`, it should also follow the x-y-z order. For additional details see [this jupyter notebook](https://github.com/InsightSoftwareConsortium/SimpleITK-Notebooks/blob/master/Python/03_Image_Details.ipynb).

Fiji is not aware of variable image spacing, assumes isotropic pixels/voxels ([1,1,1]). Other programs that do account for image spacing can be used via the `Show` method (e.g. ITK-SNAP, 3D Slicer). See [this jupyter notebook](https://github.com/InsightSoftwareConsortium/SimpleITK-Notebooks/blob/b75721a121102cf972f942fad927751089a7cc80/Python/04_Image_Display.ipynb) for details.

---

<div class="post-metadata">

**Author:** ![Moha\_data](https://discourse.itk.org/letter_avatar_proxy/v4/letter/m/ecc23a/32.png) [@Moha\_data](https://discourse.itk.org/u/Moha_data)\
**Post date:** [October 6, 2021, 10:36pm UTC](https://discourse.itk.org/t/convert-from-3d-numpy-array-to-simple-itk/4468/3 "2021-10-06T22:36:54Z")

</div>

Thanks @zivy for the comment.  
So, assuming we have an `Image_array` with corresponding `spacing`, we can use the below code to convert it to `sitk image`:

`image_sitk = sitk.GetImageFromArray(image_array)`  
` image_sitk.SetOrigin((0, 0, 0))`  
`spacing.reverse()`  
`image_sitk.SetSpacing(spacing)`

and after any modification in sitk, we can use:

modified\_image = sitk.GetArrayFromImage(modified\_image\_sitk)`

and `spacing.reverse()` to reorder the `spacing` to be used in NumPy.

---

<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:** [October 6, 2021, 11:43pm UTC](https://discourse.itk.org/t/convert-from-3d-numpy-array-to-simple-itk/4468/4 "2021-10-06T23:43:05Z")

</div>

Hello @Moha_data,

Yes (assuming `spacing` is a list associated with the numpy data so `spacing.reverse()` changes it in place).
