Programmable Source Simple 2d vtkImageData Output from numpy?

I’m trying to understand how to turn my numpy arrays into a 2D vtkImageData paraview element. I was hoping that the documentation here would cover it, but their example is a 3d volumetric rendering example instead https://docs.paraview.org/en/latest/ReferenceManual/pythonProgrammableFilter.html#reading-binary-2d-image.

Can someone help me create an example of loading a 64x64 random numpy float32 array into a vtkImageData output for a programmable source? From there I’m confident I can apply it to my own data.

Thanks!

Hi @Devin_Richard_Bayly

From the example you shared, removing the third component should be trivial. Did you try that ?

Best,

Hi there,

I tried the removing the third component by setting it to 0, is that what you had in mind?

#script code
import h5py

from pathlib import Path

pth = Path.home()/"Downloads/test.hdf5"
f = h5py.File(pth,"r")



data = f["data"][20]
dims = data.shape
#data = data.flatten()
output.SetExtent(0, dims[0]-1, 0, dims[1]-1,0,0)
output.PointData.append(data, "scalars")
output.PointData.SetActiveScalars("scalars")
# Code for 'RequestInformation Script'.
executive = self.GetExecutive()
outInfo = executive.GetOutputInformation(0)
# we assume the dimensions are (48, 62, 42).
outInfo.Set(executive.WHOLE_EXTENT(), 0, 63, 0, 63,0,0)
outInfo.Set(vtk.vtkDataObject.SPACING(), 1, 1,0)

For some reason it thinks all my data is the same value, when the values that are in the test.hdf5 are actually a (500,64,64) array of floats from a PDE solver. Can the images only be uint8? is that part of the problem?

Ok, nvm. There seemed to be something going on where when I didn’t flatten the data it wouldn’t associate the value with an invididual cell in the image. This seems to work for my purposes.

import h5py

from pathlib import Path
import numpy as np
pth = Path.home()/"Downloads/test.hdf5"
f = h5py.File(pth,"r")



data = f["data"][20]
dims = data.shape
rand =( np.random.random(data.shape)*255).flatten()
data = data.flatten()
print(data.shape)
output.SetExtent(0, dims[0]-1, 0, dims[1]-1,0,0)
output.PointData.append(data, "scalars")
output.PointData.SetActiveScalars("scalars")
# Code for 'RequestInformation Script'.
executive = self.GetExecutive()
outInfo = executive.GetOutputInformation(0)
# we assume the dimensions are (48, 62, 42).
outInfo.Set(executive.WHOLE_EXTENT(), 0, 63, 0, 63,0,0)
outInfo.Set(vtk.vtkDataObject.SPACING(), 1, 1,0)

I guess I thought perhaps an image was simplistic enough to not have to fill out a request information script.

If I wanted to get a comprehensive handle on all the programmable source export types and how they relate to vtk python coding what resource would you point me to?

You already shared the most comprehensive documentation about prog filter I’m afraid.

Ah, I see. Well thanks all the same for the help! I’m up an running with my data now.