# How to write a 2d image into a vtk file?

**URL:** https://discourse.vtk.org/t/how-to-write-a-2d-image-into-a-vtk-file/8666
**Category:** Support
**Created:** [June 5, 2022, 10:36am UTC](https://discourse.vtk.org/t/how-to-write-a-2d-image-into-a-vtk-file/8666 "2022-06-05T10:36:28Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Clifford](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/c/e56c9b/32.png) [@Clifford](https://discourse.vtk.org/u/Clifford)
#### Post date: [June 5, 2022, 10:36am UTC](https://discourse.vtk.org/t/how-to-write-a-2d-image-into-a-vtk-file/8666/1 "2022-06-05T10:36:28Z")

</div>

I use matplotlib to draw my results, and it looks like:  
 ![image](https://discourse.vtk.org/uploads/default/original/2X/4/4c572287d5ce9d0efbb98ce33b3cf3aaaaf39965.png)  
Is there any way to write them into a .vtk file and looks like a 3D graph?

---

<div class="post-metadata">

### Author: ![mau\_igna\_06](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mau_igna_06/32/8064_2.png) [@mau\_igna\_06](https://discourse.vtk.org/u/mau_igna_06)
#### Post date: [June 5, 2022, 1:03pm UTC](https://discourse.vtk.org/t/how-to-write-a-2d-image-into-a-vtk-file/8666/2 "2022-06-05T13:03:01Z")

</div>

In Slicer you can plot data like this:

# Get a volume from SampleData and compute its histogram

import SampleData  
import numpy as np  
volumeNode = SampleData.SampleDataLogic().downloadMRHead()  
histogram = np.histogram(arrayFromVolume(volumeNode), bins=50)

chartNode = slicer.util.plot(histogram, xColumnIndex = 1)  
chartNode.SetYAxisRangeAuto(False)  
chartNode.SetYAxisRange(0, 4e5)

Hope it helps

Mauro

---

<div class="post-metadata">

### Author: ![marcomusy](https://discourse.vtk.org/user_avatar/discourse.vtk.org/marcomusy/32/95_2.png) [@marcomusy](https://discourse.vtk.org/u/marcomusy)
#### Post date: [June 5, 2022, 1:32pm UTC](https://discourse.vtk.org/t/how-to-write-a-2d-image-into-a-vtk-file/8666/3 "2022-06-05T13:32:26Z")

</div>

> [@Clifford](#):
>
> and looks like a 3D graph?

you mean something like this?  
then you can use `vtkImageActor ` class and save its input as vtk.

 ![Screenshot 2022-06-05 at 15.16.05](https://discourse.vtk.org/uploads/default/original/2X/9/99a8dc9dad4e6872c110e60b5fde0ce3857c1445.png)

(code for the above):

```python
from vedo import Picture, show
pic = Picture("https://discourse.vtk.org/uploads/default/original/2X/4/4c572287d5ce9d0efbb98ce33b3cf3aaaaf39965.png")
pic.show(bg2="green9").close()

# Another option:
# import numpy as np
# from vedo.pyplot import plot
# data = np.random.rand(200,2)
# plot(data, marker='s', mc="green5", lw=0).show()

```

---

<div class="post-metadata">

### Author: ![Clifford](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/c/e56c9b/32.png) [@Clifford](https://discourse.vtk.org/u/Clifford)
#### Post date: [June 6, 2022, 2:46am UTC](https://discourse.vtk.org/t/how-to-write-a-2d-image-into-a-vtk-file/8666/4 "2022-06-06T02:46:47Z")

</div>

Is there a way to remove background? And let squares and stars be like 3D objects( give them a thick)

---

<div class="post-metadata">

### Author: ![marcomusy](https://discourse.vtk.org/user_avatar/discourse.vtk.org/marcomusy/32/95_2.png) [@marcomusy](https://discourse.vtk.org/u/marcomusy)
#### Post date: [June 6, 2022, 9:40am UTC](https://discourse.vtk.org/t/how-to-write-a-2d-image-into-a-vtk-file/8666/5 "2022-06-06T09:40:48Z")

</div>

but then you need to recreate the plot, for this you may use a `vtkGlyph` object.

`vedo` has this functionality, plus you can control the plot aspect ratio:

```python
import numpy as np
from vedo.pyplot import plot
from vedo import Cube, Star
data = np.random.rand(200,2)
m = Cube().scale(0.025)
p = plot(data, marker=m, mc="green5", lw=0, aspect=16/9)
s = Star().scale(0.03).extrude(0.1)
p+= plot(data[:10], marker=s, mc="red5", lw=0, like=p)
p.unpack(1).lighting("default")
p.show()

```

 ![Screenshot 2022-06-06 at 11.32.41](https://discourse.vtk.org/uploads/default/original/2X/6/6e618d777549a8a84e1ebce6abafc096fa1f17e0.png)

---

<div class="post-metadata">

### Author: ![Clifford](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/c/e56c9b/32.png) [@Clifford](https://discourse.vtk.org/u/Clifford)
#### Post date: [June 9, 2022, 11:16am UTC](https://discourse.vtk.org/t/how-to-write-a-2d-image-into-a-vtk-file/8666/6 "2022-06-09T11:16:43Z")

</div>

Thanks so much!  
Btw, can I write .vtk file through matplotlib? cuz I draw images above through matplotlib, want to figure out if there’s a way to write 3D file at the same time
