# ow to convert vtkImageData to numpy for a RGB image

**URL:** https://discourse.vtk.org/t/ow-to-convert-vtkimagedata-to-numpy-for-a-rgb-image/2387
**Category:** Support
**Created:** [January 7, 2020, 2:26pm UTC](https://discourse.vtk.org/t/ow-to-convert-vtkimagedata-to-numpy-for-a-rgb-image/2387 "2020-01-07T14:26:47Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![zhang-qiang-github](https://discourse.vtk.org/user_avatar/discourse.vtk.org/zhang-qiang-github/32/2519_2.png) [@zhang-qiang-github](https://discourse.vtk.org/u/zhang-qiang-github)
#### Post date: [January 7, 2020, 2:26pm UTC](https://discourse.vtk.org/t/ow-to-convert-vtkimagedata-to-numpy-for-a-rgb-image/2387/1 "2020-01-07T14:26:47Z")

</div>

I want to convert a vtkImageData (3 components) to numpy.

For a vtkImageData with 1 components, the following code works:

```auto
temp = vtk_to_numpy(data.GetPointData().GetScalars())
dims = data.GetDimensions()
numpy_data = temp.reshape(dims[2], dims[1], dims[0])
numpy_data = numpy_data.transpose(2,1,0)

```

However, when the number of components is 3, the above code provide a wrong image. How to fix it?

In addition, how can I covert the RGB numpy to QImage?

---

<div class="post-metadata">

### Author: ![Kenichiro-Yoshimi](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/k/ecc23a/32.png) [@Kenichiro-Yoshimi](https://discourse.vtk.org/u/Kenichiro-Yoshimi)
#### Post date: [January 8, 2020, 8:12am UTC](https://discourse.vtk.org/t/ow-to-convert-vtkimagedata-to-numpy-for-a-rgb-image/2387/2 "2020-01-08T08:12:22Z")

</div>

Hi,

The following works for me:

```python
    img_scalar = data.GetPointData().GetScalars()
    dims = data.GetDimensions()
    n_comp = img_scalar.GetNumberOfComponents()
    temp = numpy_support.vtk_to_numpy(img_scalar)
    numpy_data = temp.reshape(dims[1],dims[0],n_comp)
    numpy_data = numpy_data.transpose(0,1,2)
    numpy_data = np.flipud(numpy_data)

```

> [@zhang-qiang-github](#):
>
> In addition, how can I covert the RGB numpy to QImage?

I think this forum may not be the best for this.

---

<div class="post-metadata">

### Author: ![zhang-qiang-github](https://discourse.vtk.org/user_avatar/discourse.vtk.org/zhang-qiang-github/32/2519_2.png) [@zhang-qiang-github](https://discourse.vtk.org/u/zhang-qiang-github)
#### Post date: [January 8, 2020, 1:19pm UTC](https://discourse.vtk.org/t/ow-to-convert-vtkimagedata-to-numpy-for-a-rgb-image/2387/3 "2020-01-08T13:19:22Z")

</div>

Thank you very much!

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [January 8, 2020, 4:43pm UTC](https://discourse.vtk.org/t/ow-to-convert-vtkimagedata-to-numpy-for-a-rgb-image/2387/4 "2020-01-08T16:43:01Z")

</div>

> [@zhang-qiang-github](#):
>
> In addition, how can I covert the RGB numpy to QImage?

You can use [qimage2ndarray](https://pypi.org/project/qimage2ndarray/) as described [here](https://discourse.slicer.org/t/display-an-image-in-a-separate-window/8821/2?u=lassoan).
