# vtkImage::GetScalarPointer from Python (to numpy array)?

**URL:** https://discourse.vtk.org/t/vtkimage-getscalarpointer-from-python-to-numpy-array/324
**Category:** Support
**Created:** [February 25, 2019, 4:26pm UTC](https://discourse.vtk.org/t/vtkimage-getscalarpointer-from-python-to-numpy-array/324 "2019-02-25T16:26:47Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![estan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/estan/32/460_2.png) [@estan](https://discourse.vtk.org/u/estan)
#### Post date: [February 25, 2019, 4:26pm UTC](https://discourse.vtk.org/t/vtkimage-getscalarpointer-from-python-to-numpy-array/324/1 "2019-02-25T16:26:48Z")

</div>

It returns a `str` on the format `_00007f55d65c1010_p_void`. Any way to get a Python array to the underlying image data in a zero copy way? I looked at `numpy_support.py` but couldn’t find anything like that.

---

<div class="post-metadata">

### Author: ![estan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/estan/32/460_2.png) [@estan](https://discourse.vtk.org/u/estan)
#### Post date: [February 25, 2019, 4:36pm UTC](https://discourse.vtk.org/t/vtkimage-getscalarpointer-from-python-to-numpy-array/324/2 "2019-02-25T16:36:03Z")

</div>

Replying to self, I think I found the answer in `Common/Core/Testing/Python/TestSwigPointer.py`.

One can create a VTK array and use `SetVoidArray(..)`.

---

<div class="post-metadata">

### Author: ![estan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/estan/32/460_2.png) [@estan](https://discourse.vtk.org/u/estan)
#### Post date: [February 26, 2019, 7:04am UTC](https://discourse.vtk.org/t/vtkimage-getscalarpointer-from-python-to-numpy-array/324/3 "2019-02-26T07:04:32Z")

</div>

Worked like a charm. Excerpt from my code:

```python
    from vtk import vtkUnsignedCharArray
    from vtk.util.numpy_support import vtk_to_numpy
    ...
    array = vtkUnsignedCharArray()
    array.SetNumberOfTuples(numBytes)
    array.SetVoidArray(image.GetScalarPointer(), numBytes, 1)

    frame = vtk_to_numpy(array)

```

---

<div class="post-metadata">

### Author: ![banesullivan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/banesullivan/32/7143_2.png) [@banesullivan](https://discourse.vtk.org/u/banesullivan)
#### Post date: [February 27, 2019, 2:06am UTC](https://discourse.vtk.org/t/vtkimage-getscalarpointer-from-python-to-numpy-array/324/4 "2019-02-27T02:06:08Z")

</div>

FYI, [`vtki`](http://docs.vtki.org/en/latest/) has some very useful numpy-VTK array access helpers that might help you down the road!

say you have some image data (`vtkImageData` = `vtki.UniformGrid`) given in the example dataset:

```python
>>> import vtk
>>> from vtki import examples
>>> import numpy as np
>>> data = examples.load_uniform()
>>> isinstance(data, vtk.vtkImageData)
True
>>> isinstance(data.active_scalar, np.ndarray)
True

```

---

<div class="post-metadata">

### Author: ![cory.quammen](https://discourse.vtk.org/user_avatar/discourse.vtk.org/cory.quammen/32/6751_2.png) [@cory.quammen](https://discourse.vtk.org/u/cory.quammen)
#### Post date: [February 27, 2019, 4:55am UTC](https://discourse.vtk.org/t/vtkimage-getscalarpointer-from-python-to-numpy-array/324/5 "2019-02-27T04:55:16Z")

</div>

See Berk’s 5-part series of blog posts on VTK/NumPy integration, starting with [Improved VTK – numpy integration](https://blog.kitware.com/improved-vtk-numpy-integration/).

---

<div class="post-metadata">

### Author: ![estan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/estan/32/460_2.png) [@estan](https://discourse.vtk.org/u/estan)
#### Post date: [February 27, 2019, 7:51am UTC](https://discourse.vtk.org/t/vtkimage-getscalarpointer-from-python-to-numpy-array/324/6 "2019-02-27T07:51:28Z")

</div>

Thanks for the pointers to both of you.

In this case I could make due with just `vtk_to_numpy`. I needed to write the RGB bytes of the image to stdin of an ffmpeg process. Perhaps I could have done that without involving numpy at all (what I did was `process.stdin.write(nparray.tobytes())`)

(The image came from a vtkWindowToImage filter.)
