# How to use PyImageFilter

**URL:** <https://discourse.itk.org/t/how-to-use-pyimagefilter/4593>\
**Category:** Beginner Questions\
**Tags:** python\
**Created:** [November 18, 2021, 7:40pm UTC](https://discourse.itk.org/t/how-to-use-pyimagefilter/4593 "2021-11-18T19:40:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jtduda](https://discourse.itk.org/user_avatar/discourse.itk.org/jtduda/32/42_2.png) [@jtduda](https://discourse.itk.org/u/jtduda)\
**Post date:** [November 18, 2021, 7:40pm UTC](https://discourse.itk.org/t/how-to-use-pyimagefilter/4593/1 "2021-11-18T19:40:12Z")

</div>

Is there a working example of how to use the itk.PyImageFilter class? It seems like the key is using the SetPyGenerateData method to set a python function to do the work. I have the below toy example working, but I can’t figure out how to access the input data or set the output data within the passed python function. It does appear to get called upon “Update” however.

```auto
import itk
import sys

def myFunc():

    print("myFunc is running")
    # Fun stuff goes here?
    return(None)

img = itk.imread("in.nii.gz")
InputImageType = type(img)
OutputPixelType = itk.ctype("unsigned char")
OutputImageType = itk.Image[OutputPixelType, img.GetImageDimension()]

# PyImageFilter only wrapped for UC (2D,3D) and US (2D,3D)
caster = itk.CastImageFilter[InputImageType, OutputImageType].New(img)

filter = itk.PyImageFilter[OutputImageType, OutputImageType].New()
filter.SetPyGenerateData( myFunc )
filter.SetInput( caster.GetOutput() )
filter.Update()
outImg = filter.GetOutput()

print(type(outImg))
print(outImg)
print(itk.size(outImg))

```

---

<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:** [November 18, 2021, 8:30pm UTC](https://discourse.itk.org/t/how-to-use-pyimagefilter/4593/2 "2021-11-18T20:30:04Z")

</div>

I could not find it. Considering that class was contributed more than 10 years ago, I am not sure that an example exists, much less works. @matt.mccormick @Tom_Birdsong @brad-t-moore or @blowekamp might know more.

---

<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:** [January 20, 2022, 11:16pm UTC](https://discourse.itk.org/t/how-to-use-pyimagefilter/4593/3 "2022-01-20T23:16:07Z")

</div>

In some cases, a closure could possibly be used, but that is non-optimal.

Please take a look at this patch, which passes the filter instance to the callable:

> <https://github.com/InsightSoftwareConsortium/ITK/pull/3131>
>
> \- ENH: Wrap PyImageFilter for all wrapped scalar pixel types
> \- ENH: Pass in the …class instance to the PyImageFilter PyGenerateData
> \- ENH: Add SetPyGenerateInputRequestedRegino to PyImageFilter

Then, we can do:

```auto
def my_func(py_image_filter):
    print('the fun has started')
    input = py_image_filter.GetInput()

    output = py_image_filter.GetOutput()
    # process input, populate output

```
