# Programmable Source Simple 2d vtkImageData Output from numpy?

**URL:** https://discourse.paraview.org/t/programmable-source-simple-2d-vtkimagedata-output-from-numpy/10846
**Category:** ParaView Support
**Created:** [November 24, 2022, 3:43am UTC](https://discourse.paraview.org/t/programmable-source-simple-2d-vtkimagedata-output-from-numpy/10846 "2022-11-24T03:43:35Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Devin\_Richard\_Bayly](https://discourse.paraview.org/user_avatar/discourse.paraview.org/devin_richard_bayly/32/5533_2.png) [@Devin\_Richard\_Bayly](https://discourse.paraview.org/u/Devin_Richard_Bayly)
#### Post date: [November 24, 2022, 3:43am UTC](https://discourse.paraview.org/t/programmable-source-simple-2d-vtkimagedata-output-from-numpy/10846/1 "2022-11-24T03:43:35Z")

</div>

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](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!

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.paraview.org/user_avatar/discourse.paraview.org/mwestphal/32/17_2.png) [@mwestphal](https://discourse.paraview.org/u/mwestphal)
#### Post date: [November 24, 2022, 7:16am UTC](https://discourse.paraview.org/t/programmable-source-simple-2d-vtkimagedata-output-from-numpy/10846/2 "2022-11-24T07:16:59Z")

</div>

Hi @Devin_Richard_Bayly

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

Best,

---

<div class="post-metadata">

### Author: ![Devin\_Richard\_Bayly](https://discourse.paraview.org/user_avatar/discourse.paraview.org/devin_richard_bayly/32/5533_2.png) [@Devin\_Richard\_Bayly](https://discourse.paraview.org/u/Devin_Richard_Bayly)
#### Post date: [November 25, 2022, 10:21pm UTC](https://discourse.paraview.org/t/programmable-source-simple-2d-vtkimagedata-output-from-numpy/10846/3 "2022-11-25T22:21:24Z")

</div>

Hi there,

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

```auto
#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")

```

```auto
# 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?

---

<div class="post-metadata">

### Author: ![Devin\_Richard\_Bayly](https://discourse.paraview.org/user_avatar/discourse.paraview.org/devin_richard_bayly/32/5533_2.png) [@Devin\_Richard\_Bayly](https://discourse.paraview.org/u/Devin_Richard_Bayly)
#### Post date: [November 25, 2022, 10:31pm UTC](https://discourse.paraview.org/t/programmable-source-simple-2d-vtkimagedata-output-from-numpy/10846/4 "2022-11-25T22:31:38Z")

</div>

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.

```auto
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")

```

```auto
# 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?

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.paraview.org/user_avatar/discourse.paraview.org/mwestphal/32/17_2.png) [@mwestphal](https://discourse.paraview.org/u/mwestphal)
#### Post date: [November 28, 2022, 7:51am UTC](https://discourse.paraview.org/t/programmable-source-simple-2d-vtkimagedata-output-from-numpy/10846/5 "2022-11-28T07:51:25Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Devin\_Richard\_Bayly](https://discourse.paraview.org/user_avatar/discourse.paraview.org/devin_richard_bayly/32/5533_2.png) [@Devin\_Richard\_Bayly](https://discourse.paraview.org/u/Devin_Richard_Bayly)
#### Post date: [November 28, 2022, 5:00pm UTC](https://discourse.paraview.org/t/programmable-source-simple-2d-vtkimagedata-output-from-numpy/10846/6 "2022-11-28T17:00:43Z")

</div>

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