# Recreate 3D volume rendering from vtk python to vtk.js

**URL:** https://discourse.vtk.org/t/recreate-3d-volume-rendering-from-vtk-python-to-vtk-js/5248
**Category:** Support
**Created:** [February 24, 2021, 4:10pm UTC](https://discourse.vtk.org/t/recreate-3d-volume-rendering-from-vtk-python-to-vtk-js/5248 "2021-02-24T16:10:04Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Saptarshi\_Paul](https://discourse.vtk.org/user_avatar/discourse.vtk.org/saptarshi_paul/32/3054_2.png) [@Saptarshi\_Paul](https://discourse.vtk.org/u/Saptarshi_Paul)
#### Post date: [February 24, 2021, 4:10pm UTC](https://discourse.vtk.org/t/recreate-3d-volume-rendering-from-vtk-python-to-vtk-js/5248/1 "2021-02-24T16:10:04Z")

</div>

Hello everyone,

I was able to render a 3d Volume from 2D png image stack using vtk python. I want to create the same thing with vtk.js. Can anyone please guide me how to do that. I would really appreciate any help.  
Mostly i have issue understanding how can I do this in vtk.js

files=glob.glob(r"/filePath/vtk2-example"+r"/\*.png")  
filePath = vtk.vtkStringArray()  
files.sort(key=lambda x: int(’’.join(filter(str.isdigit, x))))  
filePath.SetNumberOfValues(len(files))  
for i in range(0,len(files),1):  
filePath.SetValue(i,files[i])  
reader = vtk.vtkPNGReader()  
reader.SetFileNames(filePath)  
reader.SetDataSpacing(2,2,2)  
reader.Update()

---

<div class="post-metadata">

### Author: ![Sebastien\_Jourdain](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sebastien_jourdain/32/100_2.png) [@Sebastien\_Jourdain](https://discourse.vtk.org/u/Sebastien_Jourdain)
#### Post date: [February 24, 2021, 5:40pm UTC](https://discourse.vtk.org/t/recreate-3d-volume-rendering-from-vtk-python-to-vtk-js/5248/2 "2021-02-24T17:40:02Z")

</div>

vtk.js mostly focus on rendering. So either you can pre-process your data and ship your 3D data as a 1D array to create and render a vtkImageData on the client side.

Another approach could be to use itk.js to read the png stack and convert it to a vtkImageData all on the client side (JS/wasm).

A third solution could be to create a WASM based application using a C++ code using VTK but compiled to run in a browser environment.

---

<div class="post-metadata">

### Author: ![Saptarshi\_Paul](https://discourse.vtk.org/user_avatar/discourse.vtk.org/saptarshi_paul/32/3054_2.png) [@Saptarshi\_Paul](https://discourse.vtk.org/u/Saptarshi_Paul)
#### Post date: [February 24, 2021, 6:02pm UTC](https://discourse.vtk.org/t/recreate-3d-volume-rendering-from-vtk-python-to-vtk-js/5248/3 "2021-02-24T18:02:02Z")

</div>

Hello Sebastien,

Thank you so much for your reply. Is it possible that I read the imgestack in python and write it to a .vti file. I am really sorry I am new to this. I also want to try the 1st approach. So current i have the 3D volume being rendered in python. So now how can I ship 3D data as a 1D array?  
Here is the code snippet:  
volume = vtk.vtkVolume()  
volume.SetMapper(volumeMapper)  
volume.SetProperty(volumeProperty)  
ren = vtk.vtkRenderer()  
ren.AddVolume(volume)  
ren.SetBackground(0, 0, 0)  
print(ren)  
renWin = vtk.vtkRenderWindow()  
renWin.AddRenderer(ren)  
renWin.SetSize(900, 900)  
More precisesly since the ren object has the information about the 3d volume data, do i use this object to write to the 1d Array. if you can show me a code snippet that would be really awesome.

Best Regards

---

<div class="post-metadata">

### Author: ![Sebastien\_Jourdain](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sebastien_jourdain/32/100_2.png) [@Sebastien\_Jourdain](https://discourse.vtk.org/u/Sebastien_Jourdain)
#### Post date: [February 24, 2021, 6:30pm UTC](https://discourse.vtk.org/t/recreate-3d-volume-rendering-from-vtk-python-to-vtk-js/5248/4 "2021-02-24T18:30:42Z")

</div>

Depending on what you plan to use as server, you can do something like [this](https://github.com/plotly/dash-vtk/blob/master/demos/synthetic-volume-rendering/app.py) using dash-vtk or if you directly have a vtkImageData from the PNG stack, you can do [that](https://github.com/plotly/dash-vtk/blob/master/demos/volume-rendering/app.py#L19-L25).

They are other solutions but as a beginner, that might be the simplest path.

---

<div class="post-metadata">

### Author: ![Saptarshi\_Paul](https://discourse.vtk.org/user_avatar/discourse.vtk.org/saptarshi_paul/32/3054_2.png) [@Saptarshi\_Paul](https://discourse.vtk.org/u/Saptarshi_Paul)
#### Post date: [February 24, 2021, 6:51pm UTC](https://discourse.vtk.org/t/recreate-3d-volume-rendering-from-vtk-python-to-vtk-js/5248/5 "2021-02-24T18:51:20Z")

</div>

Thank you Sebastien, let me try it!

---

<div class="post-metadata">

### Author: ![Saptarshi\_Paul](https://discourse.vtk.org/user_avatar/discourse.vtk.org/saptarshi_paul/32/3054_2.png) [@Saptarshi\_Paul](https://discourse.vtk.org/u/Saptarshi_Paul)
#### Post date: [February 24, 2021, 7:08pm UTC](https://discourse.vtk.org/t/recreate-3d-volume-rendering-from-vtk-python-to-vtk-js/5248/6 "2021-02-24T19:08:53Z")

</div>

Hello Sebastien, also I am using ruby on rails as server and have integrated webpack, so that I can integrate vtk.js on it for rendering. The only issue is reading the imagestack.
