# Create a 4D image with python bindings

**URL:** https://discourse.itk.org/t/create-a-4d-image-with-python-bindings/1230
**Category:** Beginner Questions
**Tags:** python, simpleitk
**Created:** [August 28, 2018, 11:54am UTC](https://discourse.itk.org/t/create-a-4d-image-with-python-bindings/1230 "2018-08-28T11:54:47Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![nicoco](https://discourse.itk.org/letter_avatar_proxy/v4/letter/n/7c8e57/32.png) [@nicoco](https://discourse.itk.org/u/nicoco)
#### Post date: [August 28, 2018, 11:54am UTC](https://discourse.itk.org/t/create-a-4d-image-with-python-bindings/1230/1 "2018-08-28T11:54:48Z")

</div>

Hello everybody,

I have a 4D numpy array and would like to write it as a 4D **non-vector** MHA image. Unfortunately, `GetImageFromArray` apparently cannot do that. I can edit the file header manually after writing to disk; it achieves the result I’m looking for, but I’m wondering if there is a cleaner way to do that, using SimpleITK ideally, but itk python bindings would be OK too.

Right now, the header looks like this:

```auto
ObjectType = Image
NDims = 3
BinaryData = True
BinaryDataByteOrderMSB = False
CompressedData = False
TransformMatrix = 1 0 0 0 1 0 0 0 1
Offset = 0 0 0
CenterOfRotation = 0 0 0
AnatomicalOrientation = RAI
ElementSpacing = 1 1 1
DimSize = 256 42 8
ElementNumberOfChannels = 256
ElementType = MET_SHORT
ElementDataFile = LOCAL

```

but I would need something like this:

```auto
ObjectType = Image
NDims = 4
BinaryData = True
BinaryDataByteOrderMSB = False
CompressedData = False
TransformMatrix = 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1
Offset = 0 0 0 0
CenterOfRotation = 0 0 0 0
AnatomicalOrientation = RAI
ElementSpacing = 1 1 1 1
DimSize = 256 256 42 8
ElementType = MET_SHORT
ElementDataFile = LOCAL

```

Thanks for you help.

---

<div class="post-metadata">

### Author: ![dchen](https://discourse.itk.org/user_avatar/discourse.itk.org/dchen/32/34_2.png) [@dchen](https://discourse.itk.org/u/dchen)
#### Post date: [August 28, 2018, 3:51pm UTC](https://discourse.itk.org/t/create-a-4d-image-with-python-bindings/1230/2 "2018-08-28T15:51:47Z")

</div>

SimpleITK in Python has a function GetImageFromArray that theoretically ought to convert a 4d numpy array to a SimpleITK image. That ought to solve your problem, but I tried it, and doesn’t work correctly for 4d image. This is a problem that needs to be rectified, obviously.

Until we can do so, here is an example that takes a 4d numpy array, extracts each 3d slice as a SimpleITK image, and then joins the slices into one 4D image.

```auto
#! /usr/bin/env python

import numpy as np
import SimpleITK as sitk

np_array = np.zeros( (10,10,10,10) )

tdim = np_array.shape[3]

slices = []

for t in range(tdim):
    slices.append( sitk.GetImageFromArray( np_array[t], False ) )

img = sitk.JoinSeries(slices)

sitk.WriteImage(img, "test.mha")
```

---

<div class="post-metadata">

### Author: ![nicoco](https://discourse.itk.org/letter_avatar_proxy/v4/letter/n/7c8e57/32.png) [@nicoco](https://discourse.itk.org/u/nicoco)
#### Post date: [August 28, 2018, 4:06pm UTC](https://discourse.itk.org/t/create-a-4d-image-with-python-bindings/1230/3 "2018-08-28T16:06:24Z")

</div>

`sitk.JoinSeries` is exactly what I was after. No more ugly script to edit the MHA header!

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: [September 7, 2018, 4:19pm UTC](https://discourse.itk.org/t/create-a-4d-image-with-python-bindings/1230/4 "2018-09-07T16:19:42Z")

</div>

Thank you for reporting this issue! There is a [fix for this issue](https://github.com/SimpleITK/SimpleITK/pull/567).

This enables the following:

```auto
nda = np.arange(210, dtype=np.float32).reshape([3,5,7,2])
img = sitk.GetImageFromArray(nda, isVector=True)
print(img.GetSize()) # (2,7,5,3)

```

More improvements to the `GetImageFromArray` are planned…
