# Help Needed with VTK and ParaView: Converting, Saving, and Rendering 3D Numpy Array

**URL:** https://discourse.paraview.org/t/help-needed-with-vtk-and-paraview-converting-saving-and-rendering-3d-numpy-array/13526
**Category:** ParaView Support
**Tags:** opengl, vtk, python
**Created:** [December 16, 2023, 9:04pm UTC](https://discourse.paraview.org/t/help-needed-with-vtk-and-paraview-converting-saving-and-rendering-3d-numpy-array/13526 "2023-12-16T21:04:05Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![Davidlbit](https://discourse.paraview.org/user_avatar/discourse.paraview.org/davidlbit/32/13170_2.png) [@Davidlbit](https://discourse.paraview.org/u/Davidlbit)
#### Post date: [December 16, 2023, 9:04pm UTC](https://discourse.paraview.org/t/help-needed-with-vtk-and-paraview-converting-saving-and-rendering-3d-numpy-array/13526/1 "2023-12-16T21:04:05Z")

</div>

Hi everyone,

I’m a beginner seeking guidance on using VTK and ParaView. I have a specific problem and would appreciate your help.

**Objective:**

1. Convert a 3D numpy array to a VTI file.
2. Save the VTI file to the hard drive.
3. Display the 3D volume in ParaView.  
3.1. Ideally, utilize the GPU for enhanced rendering.

**Data:**

1. The 3D volume (numpy array) comprises grayscale images.
2. Image dimensions: 1028 x 1028.
3. The numpy array is created by stacking consecutive images.

**System Details:**

- OS: Pop OS 22.04
- GPU: Nvidia Geforce 3050 ti
- Kernel: 6.6.7-1-liquorix-amd64 (latest version)

**Problem:**  
Objectives 1 and 2 work as intended using the code provided below. However, ParaView encounters issues rendering my volume. Unless my code or the way I try to render the data is faulty.

Anyways, upon loading the data and changing the representation from outline to volume, I receive the following error message:

> ERROR: In ./VTK/Rendering/VolumeOpenGL2/vtkVolumeTexture.cxx, line 948  
> vtkVolumeTexture (0x55d97d59e950): Capabilities check via proxy texture 3D allocation failed!

> **Which leaves me with a transparent outline of a box with white borders.**

**Code for Numpy to VTK Conversion & Local Saving:**

_Code insipiration comes from this post: [here](https://discourse.slicer.org/t/how-to-convert-3d-numpy-array-to-vtk-and-save-the-vtk-file/22327/8)_

```auto
import vtk
import vtk.util.numpy_support as numpy_support

def numpyToVTK(data, output_file):
    data_type = vtk.VTK_FLOAT
    shape = data.shape

    flat_data_array = data.flatten()
    vtk_data = numpy_support.numpy_to_vtk(num_array=flat_data_array, deep=True, array_type=data_type)

    img = vtk.vtkImageData()
    img.GetPointData().SetScalars(vtk_data)
    img.SetDimensions(shape[0], shape[1], shape[2])

    # Save the VTK file
    writer = vtk.vtkXMLImageDataWriter()
    writer.SetFileName(output_file)
    writer.SetInputData(img)
    writer.Write()

if __name__ == " __main__":
    output_file = 'output_volume.vti' # Use '.vti' extension for XML-based VTK files
    numpyToVTK(DATA, output_file)

```

Any help for solving my problem is highly appreciated 😃

---

<div class="post-metadata">

### Author: ![Davidlbit](https://discourse.paraview.org/user_avatar/discourse.paraview.org/davidlbit/32/13170_2.png) [@Davidlbit](https://discourse.paraview.org/u/Davidlbit)
#### Post date: [December 18, 2023, 8:53am UTC](https://discourse.paraview.org/t/help-needed-with-vtk-and-paraview-converting-saving-and-rendering-3d-numpy-array/13526/2 "2023-12-18T08:53:05Z")

</div>

**Update**

I found the following [Post](https://discourse.vtk.org/t/vtk-color-3d-image-display-error/6434), which made clear that the error originates due to the fact that I’m exceeding the RAM/VRAM on my machine. So, after shrinking the volume size I was able to load the data in Paraview as expected.

**Current display**

 ![image](https://discourse.paraview.org/uploads/default/original/2X/4/463506e6adf000f1cc7eaa38c3b45874505dc3aa.jpeg)

**Remaining problem:**  
The data doesn’t display the true volume representation, which opens the question if it’s **a)** the fault of my python script and how I convert the images or **b)** I’m missing some additional steps I need to perform in the Paraview software.

---

<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: [December 18, 2023, 8:54am UTC](https://discourse.paraview.org/t/help-needed-with-vtk-and-paraview-converting-saving-and-rendering-3d-numpy-array/13526/3 "2023-12-18T08:54:14Z")

</div>

> [@Davidlbit](#):
>
> The data doesn’t display the true volume,

What makes you say that ?

---

<div class="post-metadata">

### Author: ![Davidlbit](https://discourse.paraview.org/user_avatar/discourse.paraview.org/davidlbit/32/13170_2.png) [@Davidlbit](https://discourse.paraview.org/u/Davidlbit)
#### Post date: [December 18, 2023, 9:00am UTC](https://discourse.paraview.org/t/help-needed-with-vtk-and-paraview-converting-saving-and-rendering-3d-numpy-array/13526/5 "2023-12-18T09:00:00Z")

</div>

Perhaps my expression is not quite clear, but what I’m trying to convey is that I anticipated obtaining the RGB representation, essentially having the individual images (slices of the volumes) stacked on top of each other. However, in its current state, it appears that I’m not capturing all the features of the volume.

---

<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: [December 18, 2023, 9:01am UTC](https://discourse.paraview.org/t/help-needed-with-vtk-and-paraview-converting-saving-and-rendering-3d-numpy-array/13526/6 "2023-12-18T09:01:21Z")

</div>

Please share the resulting test.vti

---

<div class="post-metadata">

### Author: ![Davidlbit](https://discourse.paraview.org/user_avatar/discourse.paraview.org/davidlbit/32/13170_2.png) [@Davidlbit](https://discourse.paraview.org/u/Davidlbit)
#### Post date: [December 18, 2023, 9:29am UTC](https://discourse.paraview.org/t/help-needed-with-vtk-and-paraview-converting-saving-and-rendering-3d-numpy-array/13526/7 "2023-12-18T09:29:32Z")

</div>

The **test.vti** and an **example image** of how the individual slices look like are accessible via this [link](https://mega.nz/folder/yg01CRaa#laysCkYhH7mR0nurCp-0dQ)

---

<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: [December 18, 2023, 9:36am UTC](https://discourse.paraview.org/t/help-needed-with-vtk-and-paraview-converting-saving-and-rendering-3d-numpy-array/13526/8 "2023-12-18T09:36:08Z")

</div>

test.vti contains only a scalar and no RGB data

---

<div class="post-metadata">

### Author: ![Davidlbit](https://discourse.paraview.org/user_avatar/discourse.paraview.org/davidlbit/32/13170_2.png) [@Davidlbit](https://discourse.paraview.org/u/Davidlbit)
#### Post date: [December 18, 2023, 10:13am UTC](https://discourse.paraview.org/t/help-needed-with-vtk-and-paraview-converting-saving-and-rendering-3d-numpy-array/13526/9 "2023-12-18T10:13:35Z")

</div>

Again, I’ve mispoken. Sorry for the confusion. I’m interested in the **grayscale** representation, and **not the RGB** , since all the individual slices are in the grayscale color format. Hence, vti in scalar format.

---

<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: [December 18, 2023, 10:47am UTC](https://discourse.paraview.org/t/help-needed-with-vtk-and-paraview-converting-saving-and-rendering-3d-numpy-array/13526/10 "2023-12-18T10:47:19Z")

</div>

So you mean something like this ?

 ![image](https://discourse.paraview.org/uploads/default/original/2X/1/102b7bf155161414a116057b113eac22d9494205.jpeg)

---

<div class="post-metadata">

### Author: ![fashing](https://discourse.paraview.org/user_avatar/discourse.paraview.org/fashing/32/12357_2.png) [@fashing](https://discourse.paraview.org/u/fashing)
#### Post date: [March 6, 2024, 2:58am UTC](https://discourse.paraview.org/t/help-needed-with-vtk-and-paraview-converting-saving-and-rendering-3d-numpy-array/13526/11 "2024-03-06T02:58:38Z")

</div>

Thanks for the code example create vtk image date from numpy array. I just want to mention that the flatten should be in **column-major (Fortran-style) order**. i.e. `data.flatten(order='F')`. Otherwise, the data would be disturbed.

---

<div class="post-metadata">

### Author: ![jfestas](https://discourse.paraview.org/user_avatar/discourse.paraview.org/jfestas/32/13965_2.png) [@jfestas](https://discourse.paraview.org/u/jfestas)
#### Post date: [March 18, 2024, 5:48pm UTC](https://discourse.paraview.org/t/help-needed-with-vtk-and-paraview-converting-saving-and-rendering-3d-numpy-array/13526/12 "2024-03-18T17:48:59Z")

</div>

I’m having the same issue with having a representation of a full 3d box instead of the volume represented in my binary numpy array, as if the entirety of the numpy array is foreground and i’ve checked as there are no issues with the numpy. Any new ideas on how to fix this?
