imageWarp.py 1.69 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#!/usr/bin/env python

# This example shows how to combine data from both the imaging and
# graphics pipelines. The vtkMergeFilter is used to merge the data
# from each together.

import vtk
from vtk.util.misc import vtkGetDataRoot
VTK_DATA_ROOT = vtkGetDataRoot()

# Read in an image and compute a luminance value. The image is
# extracted as a set of polygons (vtkImageDataGeometryFilter). We then
# will warp the plane using the scalar (luminance) values.
reader = vtk.vtkBMPReader()
reader.SetFileName(VTK_DATA_ROOT + "/Data/masonry.bmp")
luminance = vtk.vtkImageLuminance()
17
luminance.SetInputConnection(reader.GetOutputPort())
18
geometry = vtk.vtkImageDataGeometryFilter()
19
geometry.SetInputConnection(luminance.GetOutputPort())
20
warp = vtk.vtkWarpScalar()
21
warp.SetInputConnection(geometry.GetOutputPort())
22 23 24 25 26
warp.SetScaleFactor(-0.1)

# Use vtkMergeFilter to combine the original image with the warped
# geometry.
merge = vtk.vtkMergeFilter()
27 28
merge.SetGeometryConnection(warp.GetOutputPort())
merge.SetScalarsConnection(reader.GetOutputPort())
29
mapper = vtk.vtkDataSetMapper()
30
mapper.SetInputConnection(merge.GetOutputPort())
31 32 33 34 35 36 37 38 39 40 41 42 43
mapper.SetScalarRange(0, 255)
actor = vtk.vtkActor()
actor.SetMapper(mapper)

# Create renderer stuff
ren = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)

# Add the actors to the renderer, set the background and size
ren.AddActor(actor)
44
ren.ResetCamera()
45 46 47 48 49 50 51 52 53 54 55 56 57
ren.GetActiveCamera().Azimuth(20)
ren.GetActiveCamera().Elevation(30)
ren.SetBackground(0.1, 0.2, 0.4)
ren.ResetCameraClippingRange()

renWin.SetSize(250, 250)

cam1 = ren.GetActiveCamera()
cam1.Zoom(1.4)

iren.Initialize()
renWin.Render()
iren.Start()