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

**URL:** https://discourse.vtk.org/t/how-to-convert-3d-numpy-array-to-vtk-and-save-the-vtk-file/7979
**Category:** Support
**Created:** [March 4, 2022, 8:31am UTC](https://discourse.vtk.org/t/how-to-convert-3d-numpy-array-to-vtk-and-save-the-vtk-file/7979 "2022-03-04T08:31:51Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![firminshao25](https://discourse.vtk.org/user_avatar/discourse.vtk.org/firminshao25/32/4787_2.png) [@firminshao25](https://discourse.vtk.org/u/firminshao25)
#### Post date: [March 4, 2022, 8:31am UTC](https://discourse.vtk.org/t/how-to-convert-3d-numpy-array-to-vtk-and-save-the-vtk-file/7979/1 "2022-03-04T08:31:52Z")

</div>

Hi all,I am new here.Sorry for my little knowledge about VTK.  
I have a scalar volume (3D numpy array)  
 ![image](https://discourse.vtk.org/uploads/default/original/2X/d/d8c1617660ed04a1aebedfed2ae2dc0642956ded.png)  
I want to add some information describing the image and then save it as a .vtk file as follows:  
 ![image](https://discourse.vtk.org/uploads/default/original/2X/e/edbaef4c8f67f46def9c4adc3f6b5e041d560ff9.png)

 ![image](https://discourse.vtk.org/uploads/default/original/2X/d/d836fb58a5414ee9234c6e61b984e1d0b346e59f.png)

I searched some codes below:

```auto
import vtk.util.numpy_support as numpy_support

def numpyToVTK(data):
    data_type = vtk.VTK_FLOAT
    flat_data_array = data.transpose(2,1,0).flatten()
    vtk_data = numpy_support.numpy_to_vtk(num_array=flat_data_array, deep=True, array_type=data_type)
    shape = data.shape
    img = vtk.vtkImageData()
    img.GetPointData().SetScalars(vtk_data)
    img.SetDimensions(shape[0], shape[1], shape[2])
    return img

```

I am not sure the code is right or not and I want to check the dimension of the array step by step,also how to save the file on hard-disk?  
I will appreciate it if someone can give me some illustration! Thank you for your help in advance!
