# convert nifti file to vtk polydata file

**URL:** <https://discourse.vtk.org/t/convert-nifti-file-to-vtk-polydata-file/2325>\
**Category:** Development\
**Created:** [December 20, 2019, 9:37pm UTC](https://discourse.vtk.org/t/convert-nifti-file-to-vtk-polydata-file/2325 "2019-12-20T21:37:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nour](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/n/df788c/32.png) [@nour](https://discourse.vtk.org/u/nour)\
**Post date:** [December 20, 2019, 9:37pm UTC](https://discourse.vtk.org/t/convert-nifti-file-to-vtk-polydata-file/2325/1 "2019-12-20T21:37:41Z")

</div>

I used to convert nifti file to .vtk file but what I noticed that there are many versions.  
I need `# vtk DataFile Version 3.0` but this code gives me `# vtk DataFile Version 4.2` .

```auto
import vtk
import sys

reader = vtk.vtkNIFTIImageReader()
reader.SetFileName(sys.argv[1])
reader.Update()
print (reader)

contour=vtk.vtkMarchingCubes()  
contour.SetInputData(reader.GetOutput())
contour.ComputeNormalsOn()
contour.ComputeGradientsOn()
contour.SetValue(0,0.1)
contour.Update()
 
# Write in vtk
triangle = vtk.vtkTriangleFilter()
triangle.SetInputConnection(contour.GetOutputPort())
triangle.PassVertsOff()
triangle.PassLinesOff()
 
decimation=vtk.vtkQuadricDecimation()
decimation.SetInputConnection(triangle.GetOutputPort())
 
clean=vtk.vtkCleanPolyData()
clean.SetInputConnection(triangle.GetOutputPort())
 
triangle2 = vtk.vtkTriangleFilter()
triangle2.SetInputConnection(clean.GetOutputPort())
triangle2.PassVertsOff()
triangle2.PassLinesOff()

#save .vtk polydata
writer = vtk.vtkPolyDataWriter()
writer.SetFileTypeToASCII()
writer.SetInputConnection(contour.GetOutputPort())
writer.SetFileName("1.vtk")
writer.Write()

```

what I get after runnig this code :  
 ![80367805_557148228180036_6815696929305919488_n](https://discourse.vtk.org/uploads/default/original/2X/e/e54587d93c293a4f649283f8b6f3dc1af2a9bfb6.png)  
but what I need is ![80358453_747858695691280_844619938827075584_n](https://discourse.vtk.org/uploads/default/original/2X/7/7c9f7e5302f5a632a6d6db3b86ca6696d58aad68.png)

if you have noticed : the 4.2 file has 9 numbers as points but the other one 3.0 has only 3 which I have this error \<\<Details: cannot copy sequence with size 9 to array axis with dimension 3 \>\> when I want to display the image.  
could someone help me !!

---

<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:** [December 21, 2019, 2:50am UTC](https://discourse.vtk.org/t/convert-nifti-file-to-vtk-polydata-file/2325/2 "2019-12-21T02:50:44Z")

</div>

The format of how points are written did not change. In both cases, many point coordinates are written to the file, separated by spaces or newline characters. You should implement the file parser in a way that does not assume that a certain number of values are present in a line.

---

<div class="post-metadata">

**Author:** ![nour](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/n/df788c/32.png) [@nour](https://discourse.vtk.org/u/nour)\
**Post date:** [December 22, 2019, 10:39am UTC](https://discourse.vtk.org/t/convert-nifti-file-to-vtk-polydata-file/2325/3 "2019-12-22T10:39:16Z")

</div>

sorry, could you explain more please !! I’m beginner in coding python and vtk

---

<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:** [December 22, 2019, 12:41pm UTC](https://discourse.vtk.org/t/convert-nifti-file-to-vtk-polydata-file/2325/4 "2019-12-22T12:41:08Z")

</div>

The code snippet that you included should work fine. The generated files look valid, too.

If some software has trouble reading the file then fix that software yourself (by changing the VTK file parser so that it does not assume any particular number of values in a line) or ask the developers of that software to fix it.

Another option is to not use the legacy VTK file format anymore but use VTP, PLY, STL, OBJ, or other mesh file formats that VTK can write and that other software can read.

---

<div class="post-metadata">

**Author:** ![nour](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/n/df788c/32.png) [@nour](https://discourse.vtk.org/u/nour)\
**Post date:** [December 22, 2019, 1:11pm UTC](https://discourse.vtk.org/t/convert-nifti-file-to-vtk-polydata-file/2325/5 "2019-12-22T13:11:50Z")

</div>

sorry but when I want to calculate the curvature  
this is the error : vtkCurvatures (0x27685d0): No points/cells to operate on
