# How to convert 3D numpy array to vtk and save the .vtk file?

**URL:** <https://discourse.slicer.org/t/how-to-convert-3d-numpy-array-to-vtk-and-save-the-vtk-file/22327>\
**Category:** Support\
**Tags:** vtk, python\
**Created:** [March 5, 2022, 6:23pm UTC](https://discourse.slicer.org/t/how-to-convert-3d-numpy-array-to-vtk-and-save-the-vtk-file/22327 "2022-03-05T18:23:12Z")\
**Posts on this page:** 1\
**Showing post:** 8

<div class="post-metadata">

**Author:** ![user4](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/user4/32/13172_2.png) [@user4](https://discourse.slicer.org/u/user4)\
**Post date:** [June 16, 2022, 2:36am UTC](https://discourse.slicer.org/t/how-to-convert-3d-numpy-array-to-vtk-and-save-the-vtk-file/22327/8 "2022-06-16T02:36:42Z")

</div>

Thanks Ebrahim,  
In the begining,I just use python open() to write the numpy data,but later I found it is not efficient,taking too long to save large volume.  
Your way is better so I have changed the Solution.I have followed your steps to save vtk file on disk but there’s some modification.

1. Create a 3D scalar image just as an example

```auto
import numpy as np

numpy_arr = np.arange(1,61)
numpy_arr = numpy_arr.reshape(5,4,3)
numpy_arr.shape
--> (5, 4, 3)

```

1. Create a new empty volume

```auto
nodeName = "MyNewVolume"
imageSize = [5, 4, 3]
voxelType=vtk.VTK_UNSIGNED_CHAR
imageOrigin = [0.0, 0.0, 0.0]
imageSpacing = [0.1, 0.1, 0.1]
imageDirections = [[1,0,0], [0,1,0], [0,0,1]]
fillVoxelValue = 0

# Create an empty image volume, filled with fillVoxelValue
imageData = vtk.vtkImageData()
imageData.SetDimensions(imageSize)
imageData.AllocateScalars(voxelType, 1)
imageData.GetPointData().GetScalars().Fill(fillVoxelValue)

# Create volume node
volumeNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLScalarVolumeNode", nodeName)
volumeNode.SetOrigin(imageOrigin)
volumeNode.SetSpacing(imageSpacing)
volumeNode.SetIJKToRASDirections(imageDirections)
volumeNode.SetAndObserveImageData(imageData)
volumeNode.CreateDefaultDisplayNodes()

```

1. Update voxels of my volume node from a numpy array

```auto
slicer.util.updateVolumeFromArray(volumeNode,numpy_arr)

```

1. Save volumeNode to file

```auto
myStorageNode = volumeNode.CreateDefaultStorageNode()
myStorageNode.SetFileName("test_data.vtk")
myStorageNode.WriteData(volumeNode)

```

It is pretty okay when I open this test\_data.vtk to check.

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/6/1/6161cb7ea218c22ca34595395c182c6c6df6c174.png)

Also,I have loaded this file in Slicer as follows,

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/a/3/a3c0465b1dc93dce34978b8bfc0ac4ed76d27c96.png)  
Then,I get the array back from this volume in order to double check,

```auto
volumeNode = getNode('MyNewVolume')
# volumeNode
a = arrayFromVolume(volumeNode)
a.shape
--> (5, 4, 3)

(a==numpy_arr).all()
--> True

```

So, I am pretty sure the data is saved correctly as vtk file.  
Thank you very much for your help again,Ebrahim.

---

_[View the full topic](https://discourse.slicer.org/t/how-to-convert-3d-numpy-array-to-vtk-and-save-the-vtk-file/22327)._
