# Resample image python code.

**URL:** <https://discourse.itk.org/t/resample-image-python-code/408>\
**Category:** Community\
**Tags:** simpleitk, python\
**Created:** [November 10, 2017, 9:20pm UTC](https://discourse.itk.org/t/resample-image-python-code/408 "2017-11-10T21:20:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![SantaHub](https://discourse.itk.org/letter_avatar_proxy/v4/letter/s/73ab20/32.png) [@SantaHub](https://discourse.itk.org/u/SantaHub)\
**Post date:** [November 10, 2017, 9:20pm UTC](https://discourse.itk.org/t/resample-image-python-code/408/1 "2017-11-10T21:20:13Z")

</div>

Hi, i am new to the itk package. Would you have time to do a sample code on Resampling image in python?  
i got the c++ version here : [https://itk.org/Wiki/ITK/Examples/ImageProcessing/ResampleImageFilter](https://itk.org/Wiki/ITK/Examples/ImageProcessing/ResampleImageFilter)

My need is to reshape the image and plot a histogram between two reshaped images.

Thanks in advance.

---

<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:** [November 13, 2017, 4:22pm UTC](https://discourse.itk.org/t/resample-image-python-code/408/2 "2017-11-13T16:22:16Z")

</div>

Welcome to ITK, @SantaHub!

Here are a few resampling examples in Python:

[https://itk.org/ITKExamples/src/Filtering/ImageGrid/ResampleAScalarImage/Documentation.html](https://itk.org/ITKExamples/src/Filtering/ImageGrid/ResampleAScalarImage/Documentation.html)

[https://itk.org/ITKExamples/src/Filtering/ImageGrid/ResampleAVectorImage/Documentation.html](https://itk.org/ITKExamples/src/Filtering/ImageGrid/ResampleAVectorImage/Documentation.html)

See the [Quick Start Guide](https://itkpythonpackage.readthedocs.io/en/latest/Quick_start_guide.html) on how convert the image pixel buffer to a numpy array. A histogram can be visualized from the array with [matplotlib](https://matplotlib.org/1.2.1/examples/pylab_examples/histogram_demo.html).

However, I would not expect a large difference in the histogram unless there is extreme deformation in the transform.

---

<div class="post-metadata">

**Author:** ![SantaHub](https://discourse.itk.org/letter_avatar_proxy/v4/letter/s/73ab20/32.png) [@SantaHub](https://discourse.itk.org/u/SantaHub)\
**Post date:** [November 13, 2017, 7:37pm UTC](https://discourse.itk.org/t/resample-image-python-code/408/3 "2017-11-13T19:37:36Z")

</div>

Thanks @ma

---

<div class="post-metadata">

**Author:** ![kayarre](https://discourse.itk.org/user_avatar/discourse.itk.org/kayarre/32/669_2.png) [@kayarre](https://discourse.itk.org/u/kayarre)\
**Post date:** [November 24, 2019, 10:50pm UTC](https://discourse.itk.org/t/resample-image-python-code/408/4 "2019-11-24T22:50:28Z")

</div>

How would you do this for a vector image?

I think this would use something like the following:

```auto
Dimension = 2
PixelType = itk.RGBPixel[itk.UC]
ImageType = itk.Image[PixelType, Dimension]

```

the problem I think is that I want to do this in simpleITK, because otherwise I am mixing things together in a convoluted way, and having tried the above I get a type error.

either I need an equivalent SimpleITK version for RGB, or a way to convert a composite sitk transform to itk transform…

I have discovered this :

```auto
# Extract first three channels of joined image (assuming RGB)
select = sitk.VectorIndexSelectionCastImageFilter()
channel1_image = select.Execute(joined_image, 0, sitk.sitkUInt8)
channel2_image = select.Execute(joined_image, 1, sitk.sitkUInt8)
channel3_image = select.Execute(joined_image, 2, sitk.sitkUInt8)

```

I found that I can resample each component individually.

---

<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:** [November 25, 2019, 3:06pm UTC](https://discourse.itk.org/t/resample-image-python-code/408/5 "2019-11-25T15:06:53Z")

</div>

You should open a new questions, but SimpleITK’s Resample working with RGB or vector pixel types by default, so there is not need to run in on a per component basis.

---

<div class="post-metadata">

**Author:** ![kayarre](https://discourse.itk.org/user_avatar/discourse.itk.org/kayarre/32/669_2.png) [@kayarre](https://discourse.itk.org/u/kayarre)\
**Post date:** [November 25, 2019, 3:09pm UTC](https://discourse.itk.org/t/resample-image-python-code/408/6 "2019-11-25T15:09:07Z")

</div>

@blowekamp Apologies for not making a new question, I will do that.
