# Python filter example

**URL:** <https://discourse.itk.org/t/python-filter-example/1853>\
**Category:** Beginner Questions\
**Tags:** filter, example, python\
**Created:** [May 9, 2019, 12:53pm UTC](https://discourse.itk.org/t/python-filter-example/1853 "2019-05-09T12:53:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![finetjul](https://discourse.itk.org/user_avatar/discourse.itk.org/finetjul/32/538_2.png) [@finetjul](https://discourse.itk.org/u/finetjul)\
**Post date:** [May 9, 2019, 12:53pm UTC](https://discourse.itk.org/t/python-filter-example/1853/1 "2019-05-09T12:53:19Z")

</div>

Hi,

I would like to write an ITK filter in Python. Is there an example somewhere ?

Thanks,  
Julien.

---

<div class="post-metadata">

**Author:** ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)\
**Post date:** [May 9, 2019, 1:51pm UTC](https://discourse.itk.org/t/python-filter-example/1853/2 "2019-05-09T13:51:31Z")

</div>

Hello Julien,

There are many types of filters. Are you looking to write a composite filter composed of other ITK filters and you logic? Integrate other algorithms into an ITK pipeline?

If you are interested in doing operations on each pixel, visiting each pixel with an iterator is generally not efficient. If you are able to figure out how to do the operations with filter on the whole image at a time , similar to `numpy` broadcast operations the implementation can be efficient.

---

<div class="post-metadata">

**Author:** ![finetjul](https://discourse.itk.org/user_avatar/discourse.itk.org/finetjul/32/538_2.png) [@finetjul](https://discourse.itk.org/u/finetjul)\
**Post date:** [May 9, 2019, 3:15pm UTC](https://discourse.itk.org/t/python-filter-example/1853/3 "2019-05-09T15:15:31Z")

</div>

Thank you Bradley,

I would like (for now) to create a composite filter (composed of other ITK filters).  
For example, I would need to overwrite the [GenerateInputRequestedRegion](https://itk.org/Doxygen/html/classitk_1_1PasteImageFilter.html#aff04469038880628657fd19ef7497a58) function.

Thanks.

---

<div class="post-metadata">

**Author:** ![fbudin](https://discourse.itk.org/user_avatar/discourse.itk.org/fbudin/32/14_2.png) [@fbudin](https://discourse.itk.org/u/fbudin)\
**Post date:** [May 9, 2019, 3:44pm UTC](https://discourse.itk.org/t/python-filter-example/1853/4 "2019-05-09T15:44:15Z")

</div>

Hello @finetjul,

Are you trying to compose existing ITK filters or use some filters and add custom steps at some point in the processing? If you are simply trying to compose filters together, I suggest you take a look at the ITK Python `pipeline` class for which I wrote an example [here](https://discourse.itk.org/t/reference-count-with-python/1483/8). Note that you may already have tried that since I wrote this example as an reply to one of your questions.  
If you need to add custom steps in your processing, you could try to still use this class but combine it with an [ITK Python `templated_class` object](https://github.com/InsightSoftwareConsortium/ITK/blob/master/Wrapping/Generators/Python/itkExtras.py#L713).  
If you are looking at actually fully writing a filter from scratch in Python, I cannot think of a good example right now, but if I think of one, I will add it here.
