# Where to contribute example?

**URL:** <https://discourse.vtk.org/t/where-to-contribute-example/14208>\
**Category:** Development\
**Tags:** python, code\
**Created:** [July 11, 2024, 8:28pm UTC](https://discourse.vtk.org/t/where-to-contribute-example/14208 "2024-07-11T20:28:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dzenanz/32/212_2.png) [@dzenanz](https://discourse.vtk.org/u/dzenanz)\
**Post date:** [July 11, 2024, 8:28pm UTC](https://discourse.vtk.org/t/where-to-contribute-example/14208/1 "2024-07-11T20:28:44Z")

</div>

Starting off from [PolyDataToImageDataStencil.py](https://gitlab.kitware.com/vtk/vtk-examples/-/blob/master/src/Python/PolyData/PolyDataToImageDataStencil.py), I created this Python example to convert input STL mesh into a 3D binary mask in MetaImage format. Where is the best place to contribute it?

```py
#!/usr/bin/env python

from vtkmodules.vtkCommonDataModel import vtkImageData
from vtkmodules.vtkIOGeometry import vtkSTLReader
from vtkmodules.vtkIOImage import vtkMetaImageWriter
from vtkmodules.vtkImagingStencil import (
    vtkImageStencil,
    vtkPolyDataToImageStencil
)

def get_program_parameters():
    import argparse
    parser = argparse.ArgumentParser(description='Converts the polydata to imagedata.')
    parser.add_argument('filename', help='A filename e.g. headMesh.stl')
    args = parser.parse_args()
    return args.filename

def main():
    mesh_filename = get_program_parameters()

    reader = vtkSTLReader()
    reader.SetFileName(mesh_filename)
    reader.Update()
    mesh = reader.GetOutput()
    bounds = mesh.GetBounds()

    spacing1 = 0.1
    pixel_padding = 5
    origin_shift = pixel_padding * spacing1
    spacing = [spacing1, spacing1, spacing1]
    origin = [bounds[0] - origin_shift, bounds[2] - origin_shift, bounds[4] - origin_shift]
    extent = [0, int((bounds[1] - bounds[0]) / spacing1) + 2 * pixel_padding, 0,
              int((bounds[3] - bounds[2]) / spacing1) + 2 * pixel_padding, 0,
              int((bounds[5] - bounds[4]) / spacing1) + 2 * pixel_padding]

    blank_image = vtkImageData()
    blank_image.SetExtent(extent)
    blank_image.AllocateScalars(3, 1) # VTK_UNSIGNED_CHAR, 1 component
    blank_image.GetPointData().GetScalars().Fill(0)
    blank_image.SetSpacing(spacing)
    blank_image.SetOrigin(origin)

    dataToStencil = vtkPolyDataToImageStencil()
    dataToStencil.SetInputData(mesh)
    dataToStencil.SetOutputSpacing(spacing)
    dataToStencil.SetOutputOrigin(origin)

    stencil = vtkImageStencil()
    stencil.SetInputData(blank_image)
    stencil.SetStencilConnection(dataToStencil.GetOutputPort())
    stencil.ReverseStencilOn()
    stencil.SetBackgroundValue(255)
    stencil.Update()
    mask = stencil.GetOutput()

    writer = vtkMetaImageWriter()
    writer.SetFileName("output.mha")
    writer.SetInputData(mask)
    writer.Write()

if __name__ == ' __main__':
    main()

```

---

<div class="post-metadata">

**Author:** ![ben.boeckel](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.vtk.org/u/ben.boeckel)\
**Post date:** [July 11, 2024, 9:02pm UTC](https://discourse.vtk.org/t/where-to-contribute-example/14208/2 "2024-07-11T21:02:33Z")

</div>

@amaclean

---

<div class="post-metadata">

**Author:** ![amaclean](https://discourse.vtk.org/user_avatar/discourse.vtk.org/amaclean/32/224_2.png) [@amaclean](https://discourse.vtk.org/u/amaclean)\
**Post date:** [July 11, 2024, 10:59pm UTC](https://discourse.vtk.org/t/where-to-contribute-example/14208/3 "2024-07-11T22:59:52Z")

</div>

@dzenanz It looks like you are almost there!

New examples for [vtk-examples](https://examples.vtk.org/site/) are always welcome, please follow instructions here: [ForDevelopers](https://examples.vtk.org/site/Instructions/ForDevelopers/) and follow the [guidelines](https://examples.vtk.org/site/Instructions/Guidelines/).  
This looks like it should go in `src/Python/IO`.

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dzenanz/32/212_2.png) [@dzenanz](https://discourse.vtk.org/u/dzenanz)\
**Post date:** [July 12, 2024, 2:41pm UTC](https://discourse.vtk.org/t/where-to-contribute-example/14208/4 "2024-07-12T14:41:14Z")

</div>

PR created: [357](https://gitlab.kitware.com/vtk/vtk-examples/-/merge_requests/357).
