[vtkusers] Python: numpy array->vtkImage->paraview
Justin Weber
onlyjus at gmail.com
Fri Mar 9 12:17:33 EST 2012
Greetings,
The story: I started using python/numpy to perform data manipulation (3D
connected components, etc..) and processing of 3D arrays. Next I managed to
use vtk to perform volume rendering etc. to visualize the data. Now, I want
to save the vtk data to a format that can be used in paraview so that a
side by side comparison with CFD simulations can be performed. I am having
trouble with saving the vtk data to a file to be used in paraview.
System: Windows 7, Python 2.7, vtkVersion:04859120.
1) Numpy array to vtk (example
here<http://www.vtk.org/Wiki/VTK/Examples/Python/vtkWithNumpy>)
works fine:
data=np.array() #sum numpy array dim=20x20x40
dataImporter = vtk.vtkImageImport()
data_string = data_matrix.tostring()
dataImporter.CopyImportVoidPointer(data_string, len(data_string))
dataImporter.SetDataScalarTypeToUnsignedChar()
dataImporter.SetNumberOfScalarComponents(1)
dataImporter.SetDataExtent(0, 20, 0, 20, 0, 40)
dataImporter.SetWholeExtent(0, 20, 0, 20, 0, 40)
2) Displaying the volume (example
here<http://www.vtk.org/Wiki/VTK/Examples/Python/vtkWithNumpy>)
works fine:
# This class describes how the volume is rendered (through ray tracing).
compositeFunction = vtk.vtkVolumeRayCastCompositeFunction()
# We can finally create our volume. We also have to specify the data for
it, as well as how the data will be rendered.
volumeMapper = vtk.vtkVolumeRayCastMapper()
volumeMapper.SetVolumeRayCastFunction(compositeFunction)
volumeMapper.SetInputConnection(dataImporter.GetOutputPort())
# The class vtkVolume is used to pair the preaviusly declared volume as
well as the properties to be used when rendering that volume.
volume = vtk.vtkVolume()
volume.SetMapper(volumeMapper)
volume.SetProperty(volumeProperty)
etc....
3) Writing data to some vtk file (can't figure out how to do this):
writer=vtk.vtkImageWriter()
writer.SetInputConnection(dataImporter.GetOutputPort())
writer.SetFileName('./test.vtk')
writer.Update()
writer.Write()
When i try opening the test.vtk file with paraview, there is no information
in it.
Any help would be much appreciated!
Thanks
Justin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20120309/4193cfc2/attachment.htm>
More information about the vtkusers
mailing list