# From vtkRenderWindow to OpenCV Mat using python

**URL:** https://discourse.vtk.org/t/from-vtkrenderwindow-to-opencv-mat-using-python/10508
**Category:** Support
**Tags:** training, python, code
**Created:** [January 19, 2023, 6:42pm UTC](https://discourse.vtk.org/t/from-vtkrenderwindow-to-opencv-mat-using-python/10508 "2023-01-19T18:42:31Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![TartaPise](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/t/47e85d/32.png) [@TartaPise](https://discourse.vtk.org/u/TartaPise)
#### Post date: [January 19, 2023, 6:42pm UTC](https://discourse.vtk.org/t/from-vtkrenderwindow-to-opencv-mat-using-python/10508/1 "2023-01-19T18:42:31Z")

</div>

Hi guys, I’ve tried many different ways to transform a vtkRenderWindow into an OpenCV Mat but I could get it and the python wrap documentation is not very solid (or am I missing some websites?).

Do you know how could I get that?

I tried something like this:

image\_data=renWin.GetRGBAPixelData(0, 0, width-1, height-1, 1)

but returns null,

I tried with vtkWindowToImageFilter but the passage from window\_to\_image\_filter.GetOutput() to numpy array or np.frombuffer fails everytime.

Can you please point me some other direction?

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [January 20, 2023, 2:37pm UTC](https://discourse.vtk.org/t/from-vtkrenderwindow-to-opencv-mat-using-python/10508/2 "2023-01-20T14:37:12Z")

</div>

Hi,

> [@TartaPise](#):
>
> I tried with vtkWindowToImageFilter but the passage from window\_to\_image\_filter.GetOutput() to numpy array or np.frombuffer fails everytime.

The documentation highlights the unusual behavior of that class:

> A [vtkWindow](https://vtk.org/doc/nightly/html/classvtkWindow.html) doesn’t behave like other parts of the VTK pipeline: **its modification time doesn’t get updated when an image is rendered**. As a result, naive use of [vtkWindowToImageFilter](https://vtk.org/doc/nightly/html/classvtkWindowToImageFilter.html) will produce an image of the first image that the window rendered, but which is never updated on subsequent window updates. This behavior is unexpected and in general undesirable.  
> **To force an update** of the output image, **call** [vtkWindowToImageFilter](https://vtk.org/doc/nightly/html/classvtkWindowToImageFilter.html)’s **`Modified()`** method after rendering to the window.

(emphasis added)

Source: [VTK: vtkWindowToImageFilter Class Reference](https://vtk.org/doc/nightly/html/classvtkWindowToImageFilter.html#details)

So, please, make sure you did as recommended in the docs.

I hope that helps,

Paulo
