# vtk complex array

**URL:** https://discourse.vtk.org/t/vtk-complex-array/1747
**Category:** Support
**Created:** [September 11, 2019, 11:11am UTC](https://discourse.vtk.org/t/vtk-complex-array/1747 "2019-09-11T11:11:03Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![shay](https://discourse.vtk.org/user_avatar/discourse.vtk.org/shay/32/874_2.png) [@shay](https://discourse.vtk.org/u/shay)
#### Post date: [September 11, 2019, 11:11am UTC](https://discourse.vtk.org/t/vtk-complex-array/1747/1 "2019-09-11T11:11:03Z")

</div>

Hey,

i’m new to vtk. i try to make a vtk image that contains a vtk complex array to create a nifti file with complex values in python. i used vtk.util.numpy\_to\_vtk to create an array, i know the function don’t support complex numbers and i was wondering if there is a way to fill the array with complex nubmers.

this is the code i’m currently using:

import numpy as np  
from vtk.util import numpy\_support

array = np.concatenate((np.real(numpy\_array), np.imag(numpy\_array)), axis=3)  
stacked\_array = array.reshape(-1, array.shape[-1])  
vtk\_array = numpy\_support.numpy\_to\_vtk(stacked\_array, deep=True, array\_type=vtk.VTK\_FLOAT)  
vtk\_image = vtk.vtkImageData()  
vtk\_image.SetDimensions(numpy\_array.shape[0], numpy\_array.shape[1], numpy\_array.shape[2])  
vtk\_image.GetPointData().SetScalars(vtk\_array)  
writer = vtk.vtkNIFTIImageWriter()  
writer.SetFileName(file\_name)  
writer.SetInputData(vtk\_image)  
writer.Write()

Thanks,  
Shay

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [September 11, 2019, 1:18pm UTC](https://discourse.vtk.org/t/vtk-complex-array/1747/2 "2019-09-11T13:18:45Z")

</div>

See [vtkNIFTIImageWriter documentation](https://vtk.org/doc/nightly/html/classvtkNIFTIImageWriter.html#details): _Images of type float or double that have 2 components will automatically be written as complex values._ So, you need to convert your 3D array of complex numbers to a 4D scalar array before calling `numpy_to_vtk`.

---

<div class="post-metadata">

### Author: ![shay](https://discourse.vtk.org/user_avatar/discourse.vtk.org/shay/32/874_2.png) [@shay](https://discourse.vtk.org/u/shay)
#### Post date: [September 11, 2019, 2:22pm UTC](https://discourse.vtk.org/t/vtk-complex-array/1747/3 "2019-09-11T14:22:32Z")

</div>

Thanks for the quick reply. i forgot to mention that my numpy array is a 4D array since it’s a vector with complex components. I tried to make it 5D array as you suggested, but i don’t know how to set the number of the vector components in the vtk\_image (I guess.vtk\_image.SetDimensions only set the spatial dimensions)

Thanks

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [September 11, 2019, 3:16pm UTC](https://discourse.vtk.org/t/vtk-complex-array/1747/4 "2019-09-11T15:16:03Z")

</div>

It has to be a 4D array (x, y, z, c). VTK can only deal with volumes up to 4 dimensions.
