Extract Cell types from mesh in Paraview

It is often usefull to select cells by their type for visualization in Paraview. Visualizing boundary layer meshes for example or extracting the surface elements of a mesh can be done by using the following script in the Programmable filter of Paraview.

input = self.GetUnstructuredGridInput()
output = self.GetUnstructuredGridOutput();

# copy all curent data
output.GetPointData().PassData(input.GetPointData())
output.GetCellData().PassData(input.GetCellData())

# create an array holding the VTK cell type id of each element
cellTypes = vtk.vtkUnsignedCharArray()
cellTypes.DeepCopy(input.GetCellTypesArray())
cellTypes.SetName("Cell Types")
output.GetCellData().AddArray(cellTypes)

Even simpler you can import this file (export_cell_types.cpd) from the menu Tools>Manage Custom Filters… > Import.

[Read More]

Simple macros in ParaView

If you use ParaView often you may want to automate some of your workflows. One way to do this is writing a macro using the Python interpreter that ships with ParaView. Here, is an example of extracting all elements from a tetrahedral mesh that have a minimum dihedral angle less that 5 degrees.

from paraview.simple import *
#### disable automatic camera reset on 'Show'
paraview.simple._DisableFirstRenderCameraReset()
# get selection
G = GetActiveSource()
# create quality Data
# set min dihedraql angle as criterion
M = MeshQuality(Input=G, TetQualityMeasure="Minimum Dihedral Angle")
# select it
SetActiveSource(M)
# Create selector
S = SelectionQuerySource(QueryString = "Quality <=5")
# Create Extractor
E = ExtractSelection(Input = M,Selection = S)
Show()
Render()

#### uncomment the following to render all views
# RenderAllViews()
# alternatively, if you want to write images, you can use SaveScreenshot(...).

Another advantage of this approach is that you can run it in a headless environment using the pvpython interpreter that ships with ParaView.

[Read More]

Removing white pixels from figures

Quite often, especially when you are working with presentations, you may realize that you need to remove the white background from your figures. One way is to use any photo editing software like Gimp for example and remove the background. But if you have a lot of files and you don’t need smoothing or any other effect you can also use a simple script like this:

#!/usr/bin/env python 
#source : http://stackoverflow.com/a/765829
from PIL import Image
import sys
white = (255, 255, 255, 255)
transparent = (255, 255, 255, 0)
for infile in sys.argv[1:]:
    img = Image.open(infile)
    img = img.convert("RGBA")

    pixdata = img.load()

    for y in xrange(img.size[1]):
        for x in xrange(img.size[0]):
            if pixdata[x, y] == white:
                pixdata[x, y] = transparent

    img.save(infile + "_modified", "PNG")

source

[Read More]