# How to use ImageRegionIterator and ImageRegionConstIteratorWithIndex in python

**URL:** <https://discourse.itk.org/t/how-to-use-imageregioniterator-and-imageregionconstiteratorwithindex-in-python/5346>\
**Category:** Community\
**Created:** [September 20, 2022, 4:50am UTC](https://discourse.itk.org/t/how-to-use-imageregioniterator-and-imageregionconstiteratorwithindex-in-python/5346 "2022-09-20T04:50:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cnkhanh](https://discourse.itk.org/letter_avatar_proxy/v4/letter/c/2acd7d/32.png) [@cnkhanh](https://discourse.itk.org/u/cnkhanh)\
**Post date:** [September 20, 2022, 4:50am UTC](https://discourse.itk.org/t/how-to-use-imageregioniterator-and-imageregionconstiteratorwithindex-in-python/5346/1 "2022-09-20T04:50:17Z")

</div>

I tried to write something like the below using python, I can’t find equivalence classes in python.  
Is there any way to do it in python?

typedef itk::ImageRegionIterator\< OutputImageType \> OutIteratorType;  
IteratorType fixedImageI( fixedImage, fixedImageRegion );

typedef itk::ImageRegionIterator\< OutputImageType \> OutIteratorType;  
OutIteratorType sAirTrappingI(sAirTrapping,fixedImageRegion);

---

<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:** [September 20, 2022, 11:53am UTC](https://discourse.itk.org/t/how-to-use-imageregioniterator-and-imageregionconstiteratorwithindex-in-python/5346/2 "2022-09-20T11:53:30Z")

</div>

There are no iterators in Python. Pixel iteration is quite inefficient in Python. The closest equivalent is using `value = image.GetPixel(index)` and `image.SetPixel(index, value)`.

---

<div class="post-metadata">

**Author:** ![cnkhanh](https://discourse.itk.org/letter_avatar_proxy/v4/letter/c/2acd7d/32.png) [@cnkhanh](https://discourse.itk.org/u/cnkhanh)\
**Post date:** [September 21, 2022, 4:04am UTC](https://discourse.itk.org/t/how-to-use-imageregioniterator-and-imageregionconstiteratorwithindex-in-python/5346/3 "2022-09-21T04:04:24Z")

</div>

> [@cnkhanh](#):
>
> typedef itk::ImageRegionIterator\< OutputImageType \> OutIteratorType;

Could you please give me an example about the equivalent of the below in Python? I am a newbie in this field, I do not understand it clearly

```auto
RegionType fixedImageRegion=fixedImage->GetBufferedRegion();

IteratorType fixedImageI( fixedImage, fixedImageRegion );
....
++fixedImageI

```

---

<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:** [September 21, 2022, 12:03pm UTC](https://discourse.itk.org/t/how-to-use-imageregioniterator-and-imageregionconstiteratorwithindex-in-python/5346/4 "2022-09-21T12:03:45Z")

</div>

Take a look at this [example](https://examples.itk.org/src/core/common/setpixelvalueinoneimage/documentation), it has both C++ and Python code.

---

<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:** [September 21, 2022, 12:05pm UTC](https://discourse.itk.org/t/how-to-use-imageregioniterator-and-imageregionconstiteratorwithindex-in-python/5346/5 "2022-09-21T12:05:30Z")

</div>

If you want to iterate over the entire image, the nested for loops is the usual approach in Python:

```nohighlight
for k in range (0, zSize):
  for j in range (0, ySize):
    for i in range (0, xSize):
      image.SetPixel(...)

```
