# Importing image from array and axis reorder

**URL:** <https://discourse.itk.org/t/importing-image-from-array-and-axis-reorder/1192>\
**Category:** Beginner Questions\
**Tags:** numpy, python\
**Created:** [August 9, 2018, 9:59pm UTC](https://discourse.itk.org/t/importing-image-from-array-and-axis-reorder/1192 "2018-08-09T21:59:49Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![fedorov](https://discourse.itk.org/user_avatar/discourse.itk.org/fedorov/32/207_2.png) [@fedorov](https://discourse.itk.org/u/fedorov)\
**Post date:** [August 9, 2018, 9:59pm UTC](https://discourse.itk.org/t/importing-image-from-array-and-axis-reorder/1192/1 "2018-08-09T21:59:49Z")

</div>

I am creating image from a numpy array, using itk-python, and I see that it reorders the array axes:

```python
print("Array shape: "+str(scanArray.shape))
scanVolume = itk.GetImageFromArray(scanArray)
print("Image shape: "+str(scanVolume.GetLargestPossibleRegion().GetSize()))

```

Result:

```nohighlight
Array shape: (512, 512, 133)
Image shape: itkSize3 ([133, 512, 512])

```

So I then thought I can swap axes to take care of this, but the result is not what I was expected:

```python
print("Array shape: "+str(scanArray.shape))
scanArray = np.swapaxes(scanArray,0,2)
print("Array shape reoriented: "+str(scanArray.shape))
scanVolume = itk.GetImageFromArray(scanArray)
print("Image shape: "+str(scanVolume.GetLargestPossibleRegion().GetSize()))

```

Result:

```nohighlight
Array shape: (512, 512, 133)
Array shape reoriented: (133, 512, 512)
Image shape: itkSize3 ([133, 512, 512])

```

I don’t understand what is happening here, and why the shape of the ITK image does not change although I change the numpy array shape.

Can someone help what is the proper way to re-shape the array here?

---

<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:** [August 9, 2018, 10:37pm UTC](https://discourse.itk.org/t/importing-image-from-array-and-axis-reorder/1192/2 "2018-08-09T22:37:28Z")

</div>

You have created a np view. Perhaps try a deep copy with numpy.copy()?

---

<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:** [August 10, 2018, 1:38pm UTC](https://discourse.itk.org/t/importing-image-from-array-and-axis-reorder/1192/3 "2018-08-10T13:38:48Z")

</div>

It could be not a problem. Aren’t ITK indices [x,y,z], whereas numpy’s are [z][y][x]?

---

<div class="post-metadata">

**Author:** ![fedorov](https://discourse.itk.org/user_avatar/discourse.itk.org/fedorov/32/207_2.png) [@fedorov](https://discourse.itk.org/u/fedorov)\
**Post date:** [August 10, 2018, 1:48pm UTC](https://discourse.itk.org/t/importing-image-from-array-and-axis-reorder/1192/4 "2018-08-10T13:48:43Z")

</div>

> [@blowekamp](#):
>
> Perhaps try a deep copy with numpy.copy()?

Thank you, this solved the problem!

> [@dzenanz](#):
>
> It could be not a problem. Aren’t ITK indices [x,y,z], whereas numpy’s are [z][y]?

Right, but that reorder should apply both when numpy is [512,512,133] and [133,512,512], and it was swapping them only in one instance.

---

<div class="post-metadata">

**Author:** ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)\
**Post date:** [August 10, 2018, 2:00pm UTC](https://discourse.itk.org/t/importing-image-from-array-and-axis-reorder/1192/5 "2018-08-10T14:00:51Z")

</div>

ITK indices, by convention, are [i,j,k] while [NumPy indices are [k,j,i]](http://scikit-image.org/docs/dev/user_guide/numpy_images.html) by convention.

That is

```auto
itk_image.GetPixel([i,j,k])

```

is the same as

```auto
numpy_array_as_image[k,j,i]

```

With ITK 4.13.1, the `keepAxes` keyword argument to `GetArrayFromImage` can be used to reorder the array; in ITK 5, this will be `keep_axes` as [we become more PEP8 compliant / Pythonic](https://discourse.itk.org/t/pythonic-interface-to-itk-filters/1106) with the Python API. As noted in the documentation, the numpy operator here for `GetImageFromArray` to flip the image is `numpy.reshape`:

> <https://github.com/InsightSoftwareConsortium/ITK/blob/efb7fcb84f8afa835bafce729a9faf4b4c4e2242/Modules/Bridge/NumPy/wrapping/PyBuffer.i.in#L8-L19>

> <https://github.com/InsightSoftwareConsortium/ITK/blob/efb7fcb84f8afa835bafce729a9faf4b4c4e2242/Modules/Bridge/NumPy/wrapping/PyBuffer.i.in#L123-L140>

However, it is **strongly recommended** not to do this, because images will be flipped in matplotlib, etc. and it has memory usage and performance downsides.

---

<div class="post-metadata">

**Author:** ![fedorov](https://discourse.itk.org/user_avatar/discourse.itk.org/fedorov/32/207_2.png) [@fedorov](https://discourse.itk.org/u/fedorov)\
**Post date:** [August 10, 2018, 2:20pm UTC](https://discourse.itk.org/t/importing-image-from-array-and-axis-reorder/1192/6 "2018-08-10T14:20:45Z")

</div>

> [@matt.mccormick](#):
>
> With ITK 4.13.1, the `keepAxes` keyword argument to `GetArrayFromImage` can be used to reorder the array

Not available yet with `pip install` - I get 4.13.0.

> [@matt.mccormick](#):
>
> As noted in the documentation

I have another beginner question - what is the documentation location for itk-python? I googled for the documentation for `GetImageFromArray`, but could only find pointers to examples and mailing list posts. I now also searched for it on ITK doxygen, and did not find it there either.

---

<div class="post-metadata">

**Author:** ![fedorov](https://discourse.itk.org/user_avatar/discourse.itk.org/fedorov/32/207_2.png) [@fedorov](https://discourse.itk.org/u/fedorov)\
**Post date:** [August 10, 2018, 2:22pm UTC](https://discourse.itk.org/t/importing-image-from-array-and-axis-reorder/1192/7 "2018-08-10T14:22:51Z")

</div>

> [@matt.mccormick](#):
>
> However, it is **strongly recommended** not to do this

Sorry, another question - if it is strongly not recommended to use that parameter to flip the image, then what is the recommended approach to re-orient/reshape/re-flip the image so that it has proper orientation when saved in an ITK-readable volumetric format?

---

<div class="post-metadata">

**Author:** ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)\
**Post date:** [August 10, 2018, 2:24pm UTC](https://discourse.itk.org/t/importing-image-from-array-and-axis-reorder/1192/8 "2018-08-10T14:24:02Z")

</div>

> [@fedorov](#):
>
> what is the recommended approach to re-orient/reshape/re-flip the image so that it has proper orientation when saved in an ITK-readable volumetric format?

Do not reshape the image 😉 – use ITK and Python indexing conventions as appropriate.

---

<div class="post-metadata">

**Author:** ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)\
**Post date:** [August 10, 2018, 2:33pm UTC](https://discourse.itk.org/t/importing-image-from-array-and-axis-reorder/1192/9 "2018-08-10T14:33:11Z")

</div>

> [@fedorov](#):
>
> Not available yet with `pip install` - I get 4.13.0.

Sorry, this is coming, “soon” 🕓.

> [@fedorov](#):
>
> I have another beginner question - what is the documentation location for itk-python? I googled for the documentation for `GetImageFromArray` , but could only find pointers to examples and mailing list posts. I now also searched for it on ITK doxygen, and did not find it there either.

These are all good questions, @fedorov .

Google is not too helpful with documentation pointers, yet. ☹

For `GetImageFromArray`, the most convenient way to get help is

```auto
help(itk.GetImageFromArray)

```

or, in IPython / Jupyter

```auto
itk.GetImageFromArray?

```

There is also the [ITK Python Quick Start Guide](https://itkpythonpackage.readthedocs.io/en/latest/Quick_start_guide.html) and the [Python section of the ITK Software Guide](https://itk.org/ITKSoftwareGuide/html/Book1/ITKSoftwareGuide-Book1ch3.html#x39-440003.7).

---

<div class="post-metadata">

**Author:** ![fedorov](https://discourse.itk.org/user_avatar/discourse.itk.org/fedorov/32/207_2.png) [@fedorov](https://discourse.itk.org/u/fedorov)\
**Post date:** [August 10, 2018, 2:38pm UTC](https://discourse.itk.org/t/importing-image-from-array-and-axis-reorder/1192/10 "2018-08-10T14:38:16Z")

</div>

> [@matt.mccormick](#):
>
> Do not reshape the image

Hmmm … Ok, let me give you a bit more details on what I need to do then!

I have an image (CT series) coming from DICOM. I have the segmentation of that CT series coming from another python-based tool. I want the two to line up in Slicer.

I think “do not reshape the image” is not the answer I am looking for here! 😉

---

<div class="post-metadata">

**Author:** ![fedorov](https://discourse.itk.org/user_avatar/discourse.itk.org/fedorov/32/207_2.png) [@fedorov](https://discourse.itk.org/u/fedorov)\
**Post date:** [August 10, 2018, 3:09pm UTC](https://discourse.itk.org/t/importing-image-from-array-and-axis-reorder/1192/11 "2018-08-10T15:09:50Z")

</div>

In case it can help someone later, I was able to recover proper orientation of the volume saved by ITK with the following 2 operators:

```python
scanArray = np.swapaxes(scanArray,0,2).copy()
scanArray = np.rollaxis(scanArray,2,1).copy()

```

---

<div class="post-metadata">

**Author:** ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)\
**Post date:** [August 10, 2018, 6:37pm UTC](https://discourse.itk.org/t/importing-image-from-array-and-axis-reorder/1192/12 "2018-08-10T18:37:26Z")

</div>

Thanks for sharing the solution.

Another option:

```auto
scanArray = np.transpose(scanArray, (2,1,0))

```

Which Python-based tool is this?

---

<div class="post-metadata">

**Author:** ![fedorov](https://discourse.itk.org/user_avatar/discourse.itk.org/fedorov/32/207_2.png) [@fedorov](https://discourse.itk.org/u/fedorov)\
**Post date:** [August 11, 2018, 7:39pm UTC](https://discourse.itk.org/t/importing-image-from-array-and-axis-reorder/1192/13 "2018-08-11T19:39:38Z")

</div>

I am working on a project to generate standard DICOM representation for the XML annotations in this collection: [https://wiki.cancerimagingarchive.net/display/Public/LIDC-IDRI](https://wiki.cancerimagingarchive.net/display/Public/LIDC-IDRI). Those XML files are already parsed by this library: [https://pylidc.github.io/](https://pylidc.github.io/), so I am using the numpy masks of the segmentation as the input for the conversion process with itk-python.

---

<div class="post-metadata">

**Author:** ![juanprietob](https://discourse.itk.org/user_avatar/discourse.itk.org/juanprietob/32/1159_2.png) [@juanprietob](https://discourse.itk.org/u/juanprietob)\
**Post date:** [December 13, 2019, 12:12am UTC](https://discourse.itk.org/t/importing-image-from-array-and-axis-reorder/1192/14 "2019-12-13T00:12:34Z")

</div>

I’ve come across another particular issue regarding how the data is read in python ITK.  
I’m using ITK to read image and store them in the tensorflow friendly format (tfrecords).  
I have trained a network using these and everything seems fine while working in python.

My issue starts when I export my trained model for deployment. I want to use the trained model in an application using itk.js. When trying to do inference using this model, the output data seems to have issues with the underlying data organization.

This is the output of the inference function in javascript

 ![image](https://discourse.itk.org/uploads/default/original/2X/7/7c670afb8232a96da9a166e626a2d848318e4eb2.jpeg)

The image looks like this after inference in python

 ![image](https://discourse.itk.org/uploads/default/original/2X/6/6c4e93b05c952947187320f313a6aad30b168c66.jpeg)

Any ideas on how this should be handled?

Thanks!

---

<div class="post-metadata">

**Author:** ![juanprietob](https://discourse.itk.org/user_avatar/discourse.itk.org/juanprietob/32/1159_2.png) [@juanprietob](https://discourse.itk.org/u/juanprietob)\
**Post date:** [December 16, 2019, 8:01pm UTC](https://discourse.itk.org/t/importing-image-from-array-and-axis-reorder/1192/15 "2019-12-16T20:01:54Z")

</div>

I figured out a way to make it work. Since itk.js and python itk have the same underlying data organization, the trick is to flip the dimensions when feeding the data to the tensor.  
Here is an example:

> ```
> const tf = require('@tensorflow/tfjs-node');
> const readImageLocalFileSync = require('itk/readImageLocalFileSync');
> const writeImageLocalFileSync = require('itk/writeImageLocalFileSync');
> 
> var inputFileName = './myImg.nrrd';
> var outputFileName = './myImgOut.nrrd';
> var inputModelDir = './myExportedModelDir';
> const useCompression = true
> 
> tf.node.loadSavedModel(inputModelDir)
> .then(function(model){
> 
> const in_img = readImageLocalFileSync(inputFileName);
> var img_size = [...in_img.size];//Copy the dimensions to a new array
> img_size.reverse();//Reverse the dimensions array
> var x = tf.tensor(Float32Array.from(in_img.data), [1, ...img_size, in_img.imageType.components]);//Reshape the tensor using the reversed array
>     
> var y = model.predict(x);
> 
> const out_img = new Image(new ImageType(2,'float'));
> out_img.origin = in_img.origin;
> out_img.spacing = in_img.spacing;
> out_img.direction = in_img.direction;
> out_img.size = in_img.size;
> 
> return y.buffer()
> .then(function(buf){
> out_img.data = buf.values;
> writeImageLocalFileSync(useCompression, out_img, outputFileName)
> });
> })
> .catch(function(err){
> console.error(err);
> })
> 
> ```
