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.
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(..)
.
Worked like a charm. Excerpt from my code:
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)
FYI, vtki
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:
>>> 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
See Berk’s 5-part series of blog posts on VTK/NumPy integration, starting with Improved VTK – numpy integration.
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.)