# TriangleMeshToBinaryImageFilter In Python

**URL:** https://discourse.itk.org/t/trianglemeshtobinaryimagefilter-in-python/1604
**Category:** Beginner Questions
**Tags:** itkv5, python
**Created:** [February 15, 2019, 9:01pm UTC](https://discourse.itk.org/t/trianglemeshtobinaryimagefilter-in-python/1604 "2019-02-15T21:01:05Z")
**Posts on this page:** 5
**Page:** 2

<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 9, 2024, 3:37pm UTC](https://discourse.itk.org/t/trianglemeshtobinaryimagefilter-in-python/1604/21 "2024-07-09T15:37:55Z")

</div>

These are the key parts from [this example](https://examples.itk.org/src/core/mesh/converttrianglemeshtobinaryimage/documentation):

```c++
using FilterType = itk::TriangleMeshToBinaryImageFilter<MeshType, OutputImageType>;
auto filter = FilterType::New();
filter->SetInput(meshReader->GetOutput());
filter->SetInfoImage(cast->GetOutput());
filter->SetInsideValue(itk::NumericTraits<OutputPixelType>::max());
filter->Update();

```

This is attempt at translating it into python:

```py
UCType = itk.image[itk.UC, 3]
ConverterType = itk.TriangleMeshToBinaryImageFilter[MeshType, UCType]
filter = ConverterType.New()
filter.SetInput(mesh) # mesh is read from file
# referenceImage is read from file or constructed by setting size, origin, and spacing
filter.SetInfoImage(referenceImage)
filter.SetInput(255)
filter.Update()
itk.imwrite(filter.GetOutput(), "mask.nrrd")

```

---

<div class="post-metadata">

### Author: ![Gregori](https://discourse.itk.org/letter_avatar_proxy/v4/letter/g/ba8739/32.png) [@Gregori](https://discourse.itk.org/u/Gregori)
#### Post date: [July 9, 2024, 3:40pm UTC](https://discourse.itk.org/t/trianglemeshtobinaryimagefilter-in-python/1604/22 "2024-07-09T15:40:09Z")

</div>

I am currently unable to still import the stl file into ITK at all to be able to do anything. This is why I was hoping for an option involving vtk, as the jupyter notebook fails every time I try to load the stl file in with ITK.

---

<div class="post-metadata">

### Author: ![dchen](https://discourse.itk.org/user_avatar/discourse.itk.org/dchen/32/34_2.png) [@dchen](https://discourse.itk.org/u/dchen)
#### Post date: [July 9, 2024, 6:15pm UTC](https://discourse.itk.org/t/trianglemeshtobinaryimagefilter-in-python/1604/23 "2024-07-09T18:15:41Z")

</div>

If you’re using VTK, you can use the vtkPolyDataToImageStencil to scan convert a mesh into a volume image.

---

<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 9, 2024, 6:53pm UTC](https://discourse.itk.org/t/trianglemeshtobinaryimagefilter-in-python/1604/24 "2024-07-09T18:53:33Z")

</div>

Example for that (also in C++) is [here](https://examples.vtk.org/site/Cxx/PolyData/PolyDataToImageData/).

---

<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 10, 2024, 2:25am UTC](https://discourse.itk.org/t/trianglemeshtobinaryimagefilter-in-python/1604/25 "2024-07-10T02:25:00Z")

</div>

> [@Gregori](#):
>
> I am currently unable to still import the stl file into ITK at all to be able to do anything.

To load an STL file:

```sh
pip install itk-iomeshstl

```

```py
import itk
mesh = itk.meshread('mesh.stl')

```

[Previous page](https://discourse.itk.org/t/trianglemeshtobinaryimagefilter-in-python/1604.md?page=1)
