WriteReadVtkImageData
vtk-examples/Python/ImageData/WriteReadVtkImageData
Question
If you have a question about this example, please use the VTK Discourse Forum
Code¶
WriteReadVtkImageData.py
#!/usr/bin/env python
# noinspection PyUnresolvedReferences
import vtkmodules.vtkInteractionStyle
# noinspection PyUnresolvedReferences
import vtkmodules.vtkRenderingOpenGL2
from vtkmodules.vtkCommonColor import vtkNamedColors
from vtkmodules.vtkCommonCore import VTK_DOUBLE
from vtkmodules.vtkCommonDataModel import vtkImageData
from vtkmodules.vtkFiltersGeometry import vtkImageDataGeometryFilter
from vtkmodules.vtkIOXML import (
vtkXMLImageDataReader,
vtkXMLImageDataWriter
)
from vtkmodules.vtkRenderingCore import (
vtkActor,
vtkPolyDataMapper,
vtkRenderWindow,
vtkRenderWindowInteractor,
vtkRenderer
)
def get_program_parameters():
import argparse
description = 'Generate image data, edit data points, store and reload it.'
epilogue = '''
'''
parser = argparse.ArgumentParser(description=description, epilog=epilogue)
parser.add_argument('filename', help='A required vtk filename, e.g. writeImageData.vti.', nargs='?',
const='writeImageData.vti',
type=str, default='writeImageData.vti')
args = parser.parse_args()
return args.filename
def main():
colors = vtkNamedColors()
filename = get_program_parameters()
imageData = vtkImageData()
imageData.SetDimensions(3, 4, 5)
imageData.AllocateScalars(VTK_DOUBLE, 1)
dims = imageData.GetDimensions()
# Fill every entry of the image data with '2.0'
for z in range(dims[2]):
for y in range(dims[1]):
for x in range(dims[0]):
imageData.SetScalarComponentFromDouble(x, y, z, 0, 2.0)
writer = vtkXMLImageDataWriter()
writer.SetFileName(filename)
writer.SetInputData(imageData)
writer.Write()
# Read the file (to test that it was written correctly)
reader = vtkXMLImageDataReader()
reader.SetFileName(filename)
reader.Update()
# Convert the image to a polydata
imageDataGeometryFilter = vtkImageDataGeometryFilter()
imageDataGeometryFilter.SetInputConnection(reader.GetOutputPort())
imageDataGeometryFilter.Update()
mapper = vtkPolyDataMapper()
mapper.SetInputConnection(imageDataGeometryFilter.GetOutputPort())
actor = vtkActor()
actor.SetMapper(mapper)
actor.GetProperty().SetPointSize(3)
# Setup rendering
renderer = vtkRenderer()
renderer.AddActor(actor)
renderer.SetBackground(colors.GetColor3d('White'))
renderer.ResetCamera()
renderWindow = vtkRenderWindow()
renderWindow.AddRenderer(renderer)
renderWindowInteractor = vtkRenderWindowInteractor()
renderWindowInteractor.SetRenderWindow(renderWindow)
renderWindowInteractor.Initialize()
renderWindowInteractor.Start()
if __name__ == '__main__':
main()