# Image type for an ITK image from numpy array

**URL:** <https://discourse.itk.org/t/image-type-for-an-itk-image-from-numpy-array/2348>\
**Category:** Beginner Questions\
**Created:** [October 24, 2019, 6:13pm UTC](https://discourse.itk.org/t/image-type-for-an-itk-image-from-numpy-array/2348 "2019-10-24T18:13:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![hina-shah](https://discourse.itk.org/user_avatar/discourse.itk.org/hina-shah/32/1001_2.png) [@hina-shah](https://discourse.itk.org/u/hina-shah)\
**Post date:** [October 24, 2019, 6:13pm UTC](https://discourse.itk.org/t/image-type-for-an-itk-image-from-numpy-array/2348/1 "2019-10-24T18:13:44Z")

</div>

Here’s a piece of code I’m trying to run using pydicom and itk (using pydicom for a better dicom tag extraction since itk was failing to get some tags).

> ds = pydicom.read\_file(file\_path)  
> np\_image = ds.pixel\_array  
> itk\_image = itk.GetImageFromArray(np.ascontiguousarray(np\_image))  
> lumfilter = itk.RGBToLuminanceImageFilter.New(Input=itk\_image)

However, `lumfilter` fails since it’s expecting an RGB image `itkImageRGBUC2`, but the image that ITK created from the array is of type `itkImageUC3`. Is `itkCastImageFilter` the only way to go, or am I missing something?

Thanks!

---

<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:** [October 24, 2019, 7:42pm UTC](https://discourse.itk.org/t/image-type-for-an-itk-image-from-numpy-array/2348/2 "2019-10-24T19:42:52Z")

</div>

Maybe `itkImageRGBUC3` is not wrapped by default, so ITK implicitly converts it to scalar?

**EDIT:** the conversion RGB-\>Scalar usually uses luminance, so in this case you don’t need `RGBToLuminanceImageFilter`.

Also, does `np.ascontiguousarray(np_image)` give an RGB image?

---

<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:** [October 24, 2019, 8:14pm UTC](https://discourse.itk.org/t/image-type-for-an-itk-image-from-numpy-array/2348/3 "2019-10-24T20:14:17Z")

</div>

To achieve explicit conversion to an RGB image, we can now call:

```auto
ImageType = itk.Image[itk.RGBPixel[itk.UC], 3]
itk_image = itk.PyBuffer[ImageType].GetImageFromArray(np_image)

```

That said, we can make this similar to the [functional ITK Python filter interfaces](https://itkpythonpackage.readthedocs.io/en/latest/Quick_start_guide.html#basic-examples), support passing a `ttype` keyword argument to the ITK NumPy interface functions. This will enable explicit specification of the type desired. For example:

```auto
ImageType = itk.Image[itk.RGBPixel[itk.UC], 3]
itk_image = itk.image_from_array(np_image, ttype=(ImageType,))

```

I created [this issue](https://github.com/InsightSoftwareConsortium/ITK/issues/1348) to track this.

> [@dzenanz](#):
>
> Also, does `np.ascontiguousarray(np_image)` give an RGB image?

This just ensures it is a contiguous buffer, which is a requirement for the conversion. But, we are looking to make the [conversions required implicit](https://github.com/InsightSoftwareConsortium/ITK/issues/1243) for ITK 5.1b02.

---

<div class="post-metadata">

**Author:** ![hina-shah](https://discourse.itk.org/user_avatar/discourse.itk.org/hina-shah/32/1001_2.png) [@hina-shah](https://discourse.itk.org/u/hina-shah)\
**Post date:** [October 25, 2019, 11:46am UTC](https://discourse.itk.org/t/image-type-for-an-itk-image-from-numpy-array/2348/4 "2019-10-25T11:46:36Z")

</div>

Thanks Matt and Dzenan!  
The PyBuffer solution worked, but the passing the `ttype` keyword argument would be great!!

Cheers!  
Hina

---

<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 29, 2021, 7:25pm UTC](https://discourse.itk.org/t/image-type-for-an-itk-image-from-numpy-array/2348/5 "2021-01-29T19:25:30Z")

</div>

Following-up, `ttype` is a keyword parameter in `itk-5.2rc1` for `itk.image_from_array`.

---

<div class="post-metadata">

**Author:** ![hina-shah](https://discourse.itk.org/user_avatar/discourse.itk.org/hina-shah/32/1001_2.png) [@hina-shah](https://discourse.itk.org/u/hina-shah)\
**Post date:** [January 29, 2021, 7:32pm UTC](https://discourse.itk.org/t/image-type-for-an-itk-image-from-numpy-array/2348/6 "2021-01-29T19:32:41Z")

</div>

Awesome!  
Thanks! 🙂

Hope you’re doing well!

Best,  
Hina
