# Reading GIFTI file in Python

**URL:** https://discourse.itk.org/t/reading-gifti-file-in-python/6052
**Category:** Beginner Questions
**Tags:** itk, python, simpleitk
**Created:** [July 10, 2023, 4:09pm UTC](https://discourse.itk.org/t/reading-gifti-file-in-python/6052 "2023-07-10T16:09:25Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![mcespedes99](https://discourse.itk.org/user_avatar/discourse.itk.org/mcespedes99/32/3543_2.png) [@mcespedes99](https://discourse.itk.org/u/mcespedes99)
#### Post date: [July 10, 2023, 4:09pm UTC](https://discourse.itk.org/t/reading-gifti-file-in-python/6052/1 "2023-07-10T16:09:25Z")

</div>

Hi,

I’m currently trying to develop an extension for 3D Slicer to read, import and convert GIFTI images. I’m developing this module in Python and I’m currently using nibabel to read the gifti files (both containing surfaces - .surf.gii or labels - label.gii) ([Neuroimaging in Python — NiBabel 5.1.0 documentation](https://nipy.org/nibabel/reference/nibabel.gifti.html)). As ITK is already installed within 3D Slicer, from a conversation that I had with Andras Lasso, I was wondering if it’s possible to read GIFTI files using ITK in Python. I was reading the documentation from SimpleITK but I didn’t find anything useful as I don’t see that GIFTI is supported here: [Reading and Writing for Images and Transforms — SimpleITK 1.2.0.dev documentation](https://simpleitk.readthedocs.io/en/v1.2.4/Documentation/docs/source/IO.html)

So does anyone know if it’s possible to read GIFTI files using ITK in Python? Thanks in advance for any help.  
-Mauricio

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.itk.org/user_avatar/discourse.itk.org/lassoan/32/27_2.png) [@lassoan](https://discourse.itk.org/u/lassoan)
#### Post date: [July 10, 2023, 4:32pm UTC](https://discourse.itk.org/t/reading-gifti-file-in-python/6052/2 "2023-07-10T16:32:25Z")

</div>

I’ve tried asking bing chat about this and it created this code that works well in 3D Slicer (after pip-installing `itk`):

```python
import itk

MeshType = itk.Mesh[itk.F, 3]
reader = itk.MeshFileReader[MeshType].New()
meshIO = itk.GiftiMeshIO.New()
reader.SetMeshIO(meshIO)
reader.SetFileName("/path/to/mymesh.gii")
reader.Update()
mesh = reader.GetOutput()

writer = itk.MeshFileWriter[MeshType].New()
writer.SetFileName("/path/to/mymesh.vtk")
writer.SetInput(mesh)
writer.Update()

```

A few questions remain:

- Is the GIFTI reader available in SimpleITK? (SimpleITK is already bundled with Slicer, so it would be nice to use it instead of installing an additional Python package)
- Is there a converter from ITK mesh (`itk.itkMeshBasePython.itkMeshF3`) type to VTK mesh (`vtkmodules.vtkCommonDataModel.vtkPolyData`) so that we can visualize the mesh using VTK without file writing&reading?

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [July 10, 2023, 5:30pm UTC](https://discourse.itk.org/t/reading-gifti-file-in-python/6052/3 "2023-07-10T17:30:40Z")

</div>

[ITKMeshToPolyData](https://github.com/InsightSoftwareConsortium/ITKMeshToPolyData) is meant to work with `vtk.js`. Potentially look at the [code there](https://github.com/InsightSoftwareConsortium/ITKMeshToPolyData/blob/master/include/itkMeshToPolyDataFilter.hxx), this [post](https://public.kitware.com/pipermail/vtkusers/2007-January/039803.html), or this [code](https://github.com/InsightSoftwareConsortium/ITKApps/blob/master/Auxiliary/vtk/vtk2itk.cxx).

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.itk.org/user_avatar/discourse.itk.org/lassoan/32/27_2.png) [@lassoan](https://discourse.itk.org/u/lassoan)
#### Post date: [July 27, 2023, 6:24pm UTC](https://discourse.itk.org/t/reading-gifti-file-in-python/6052/4 "2023-07-27T18:24:35Z")

</div>

Thank you @dzenanz (and sorry for the slow response). Your examples indicate that there is no ITK/VTK mesh converter available in Python, but it could be implemented similarly as it was done in C++ and for JavaScript.

Just for future reference, such [conversion between ITK image ↔ VTK image already exist in ITK Python wrapping extras](https://github.com/InsightSoftwareConsortium/ITK/blob/master/Wrapping/Generators/Python/itk/support/extras.py#L683), so if somebody implements ITK mesh ↔ VTK polydata conversion then it could be added to the same place.

---

<div class="post-metadata">

### Author: ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)
#### Post date: [July 27, 2023, 8:56pm UTC](https://discourse.itk.org/t/reading-gifti-file-in-python/6052/5 "2023-07-27T20:56:41Z")

</div>

> [@lassoan](#):
>
> I’ve tried asking bing cha

Bing did a pretty good job!]

But we can also be simpler / more pythonic:

```auto
import itk

mesh = itk.meshread('/path/to/mymesh.gii')
itk.meshwrite(mesh, '/path/to/mymesh.vtk')

```

which is [outlined in this documentation](https://itkpythonpackage.readthedocs.io/en/master/Quick_start_guide.html). This also includes a a reference to `numpy.ndarray` / `itk.Mesh` conversion. `itk.PolyData` and `vtkPolyData` could be implemented in a simple function where @lassoan suggests in the same way.
