# VTK file format to image file

Is there a way to convert vtk file without visualizing into a image file such as png in the bash terminal itself?

I have found that for the vtk files that I visualize to take longer time than its corresponding image file (which is made from a different file).

• do you mean save a "screenshot" as png file? Or your vtk data is already a 2D image? – lib Jul 7 '14 at 16:20
• I was trying to visualize vtk file in paraview, but it was too slow, so I converted it into png and then visualized it. So basically, when I was visualizing vtk actively it was too slow, so i wanted a method to input functions in a script and get the result as post processing, as converting all the files to png was a pain, but eventually I made a script to do that – Stephen Jacob Jul 8 '14 at 0:33

I don't know about "in the bash terminal itself", but I use python scripting with Visit. See here or here for some examples.

Below I have pasted a simple script I use to make an image of a 2D slice along y = 0.5 of a 3D cube. The script is named "script.py" and "ysolution" refers to a variable name with the .vtk file.

# invoke like so: visit -cli -nowin -s script.py ysolution-19.vtk ysolution

import os,sys
wholefile = os.getcwd() + "/" + sys.argv[-2]
fieldname = sys.argv[-1]

OpenDatabase(wholefile)
s = SliceAttributes()
s.originIntercept = 0.5
SetOperatorOptions(s)
DrawPlots()
t = SaveWindowAttributes()
t.fileName = "y19-"
SetSaveWindowAttributes(t)
n = SaveWindow()
print n

quit()


I would suggest using mayavi (distributed in pythonxy for instance) for this task. Assuming the vtk file is my_input_file.vtk and you wish to display a vector field in a cut plane using glyphs then save as png image, the following methods do the trick:

• Save your vtk as an image directly from the terminal:

\$ mayavi2 -d my_input_file.vtk -m VectorCutPlane -f MaskPoints -m Glyph \ -s "scene.save('my_image.png')"

• Or, alternatively wrap things up in an executable shell script mayavi_script.sh containing:

mayavi2 -d my_input_file.vtk \ -m VectorCutPlane \ -f MaskPoints \ -m Glyph \ -s "scene.save('my_image.png')"

In the both cases, the switches -d , -m, -f and -s stand respectively for datafile, module, filter and executable python expression. This is only an example, but many other things can be done after checking the documentation. The -o is very useful to parse the commands in off-screen mode, therefore no GUI will be opened.

• For more advanced offscreen visualization with image output, you can write and python-run a mayavi script (in the same idea suggested by Andrew T. Barker's answer for Visit) using the mlab and its pipeline module:

import mayavi.mlab as ml ml.options.offscreen = True ...