Convert VTK to raster image (Ruby or Python)
Question: I have the results of a simulation on an unstructured 2D mesh. I usually
export the results in VTK and visualize them with Paraview. This is what
results look like.

![Unstructured grid results](http://i.stack.imgur.com/xBLhU.png)

I would like to obtain a raster image from the results (with or without
interpolation) to use it as a texture for visualization in a 3D software. From
reading around I have gathered that I need to do some kind of resampling in
order to convert from the unstructured grid to a 2d regular grid for the
raster image.

VTK can export to raster, but it exports only a full scene without any defined
boundary so it requires manual tweaking to fit the image.

Ideally I would like to export only the results within the results bounding
box and 'map' them to a raster image programmatically with Ruby or Python.


Answer: This script uses paraview and creates an image perfectly centered and scaled
so that it can be used as a texture. Notice the `855` value for the vertical
size. It seems to be related to the resolution of the screen and it is needed
only on OSX according to Paraview mailing list.

It should be run to the Paraview Python interpreter `pvbatch`.

    
    
    import sys, json
    #### import the simple module from the paraview
    from paraview.simple import *
    #### disable automatic camera reset on 'Show'
    paraview.simple._DisableFirstRenderCameraReset()
    
    
    args = json.loads(sys.argv[1])
    # create a new 'Legacy VTK Reader'
    vtk_file = args["file"]
    
    data = LegacyVTKReader(FileNames=[vtk_file])
    
    # get active view
    renderView1 = GetActiveViewOrCreate('RenderView')
    # uncomment following to set a specific view size
    
    xc = float(args["center"][0])
    yc = float(args["center"][1])
    zc = float(args["center"][2])
    width = float(args["width"])
    height = float(args["height"])
    output_file = args["output_file"]
    scalar = args["scalar"]
    colormap_min = float(args["colormap_min"])
    colormap_max = float(args["colormap_max"])
    
    
    ratio = height / width
    magnification = 2
    height_p = 855 * magnification
    width_p = int(height_p * 1.0 / ratio / magnification)  
    
    renderView1.ViewSize = [width_p , height_p]
    
    # show data in view
    dataDisplay = Show(data, renderView1)
    # trace defaults for the display properties.
    dataDisplay.ColorArrayName = ['CELLS', scalar]
    # set scalar coloring
    ColorBy(dataDisplay, ('CELLS', scalar))
    # rescale color and/or opacity maps used to include current data range
    dataDisplay.RescaleTransferFunctionToDataRange(True)
    
    # get color transfer function/color map for 'irradiation'
    irradiationLUT = GetColorTransferFunction(scalar)
    # Rescale transfer function
    irradiationLUT.RescaleTransferFunction(colormap_min, colormap_max)
    irradiationLUT.LockDataRange = 1
    irradiationLUT.ColorSpace = 'RGB'
    irradiationLUT.NanColor = [0.498039, 0.0, 0.0]
    
    #changing interaction mode based on data extents
    renderView1.InteractionMode = '2D'
    renderView1.CameraPosition = [xc, yc, 10000.0 + zc]
    renderView1.CameraFocalPoint = [xc, yc, zc]
    
    # hide color bar/color legend
    dataDisplay.SetScalarBarVisibility(renderView1, False)
    
    # current camera placement for renderView1
    renderView1.InteractionMode = '2D'
    #renderView1.CameraPosition = [3.641002, 197.944122, 10001.75]
    #renderView1.CameraFocalPoint = [3.641002, 197.944122, 1.75]
    renderView1.CameraParallelScale = (height / 2.0)
    
    # save screenshot
    SaveScreenshot(output_file, magnification=magnification, quality=100, view=renderView1)
    

