# What is problem with conversion from nifti file to vtp

**URL:** <https://discourse.itk.org/t/what-is-problem-with-conversion-from-nifti-file-to-vtp/7154>\
**Category:** Beginner Questions\
**Created:** [August 7, 2024, 4:10am UTC](https://discourse.itk.org/t/what-is-problem-with-conversion-from-nifti-file-to-vtp/7154 "2024-08-07T04:10:51Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![babayka](https://discourse.itk.org/letter_avatar_proxy/v4/letter/b/b19c9b/32.png) [@babayka](https://discourse.itk.org/u/babayka)\
**Post date:** [August 7, 2024, 4:10am UTC](https://discourse.itk.org/t/what-is-problem-with-conversion-from-nifti-file-to-vtp/7154/1 "2024-08-07T04:10:51Z")

</div>

I have code that utilizes vtp and nibabel to convert read nifti file into vtp then I check how the geometry was made for vasculature through GeometryViewer of vtp examples.

```py
import nibabel as nib
import vtk
from vtkmodules.util import numpy_support
import numpy as np

def nifti_to_vtp(nifti_file, vtp_file, contour_value, time_index=0):
    img = nib.load(nifti_file)
    array = img.get_fdata()

    # Check if the array has four dimensions
    if array.ndim == 4:
      
        array = array[..., time_index]

    spacing = img.header.get_zooms()[:3]
    origin = img.affine[:3, 3]  
    print(array.shape)
    
    # Create vtkImageData from NumPy array
    vtk_image_data = vtk.vtkImageData()
    vtk_image_data.SetDimensions(array.shape)
    vtk_image_data.SetSpacing(spacing)
    vtk_image_data.SetOrigin(origin)

    vtk_array = numpy_support.numpy_to_vtk(np.ravel(array, order='F'), deep=True, array_type=vtk.VTK_FLOAT)
    vtk_image_data.GetPointData().SetScalars(vtk_array)

    # Use vtkFlyingEdges3D to extract iso-surface
    flying_edges = vtk.vtkFlyingEdges3D()
    flying_edges.SetInputData(vtk_image_data)
    flying_edges.SetValue(0, contour_value)
    flying_edges.Update()

    poly_data = flying_edges.GetOutput()

    # Write the vtkPolyData to a VTP file
    writer = vtk.vtkXMLPolyDataWriter()
    writer.SetFileName(vtp_file)
    writer.SetInputData(poly_data)
    writer.Write()

# Example usage
nifti_to_vtp(
    nifti_file="example.nii.gz",
    vtp_file="output.vtp",
    contour_value=150,
    time_index=0 
)

```

The code above is what I’m using now the problem is that for nifti image with four dimensions array.shape = (256, 256, 64, 1) It gives me vasculature where vessels get stretched from the highest slice to the lowest slice of the brain, while another nifti image with (512, 512, 100) dimensions results in a proper vasculature

 ![image](https://discourse.itk.org/uploads/default/original/2X/7/7beabf3ba1e7062ae67ee95dc7f7fc9b989322a7.png)

 ![image](https://discourse.itk.org/uploads/default/original/2X/8/8cd99b70a2b8d02088a81e3d33db96245d5b331e.png)
