Reader = vtk.vtkNIFTIImageReader()
Reader.SetFileName('./data/seg.nii')
Reader.Update()
reader = vtk.vtkSTLReader()
reader.SetFileName("D:/stress.stl")
reader.Update()
reader = vtk.vtkPolyDataReader()
reader.SetFileName('E:/AF_right.vtk') # SetFileName设置要读取的vtk文件
reader.ReadAllScalarsOn()
reader.ReadAllVectorsOn()
reader.ReadAllTensorsOn()
reader.Update()
# ReadImageData
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# noinspection PyUnresolvedReferences
import vtkmodules.vtkInteractionStyle
# noinspection PyUnresolvedReferences
import vtkmodules.vtkRenderingOpenGL2
from vtkmodules.vtkCommonColor import vtkNamedColors
from vtkmodules.vtkIOXML import vtkXMLImageDataReader
from vtkmodules.vtkRenderingCore import (
vtkActor,
vtkDataSetMapper,
vtkRenderWindow,
vtkRenderWindowInteractor,
vtkRenderer
)
# Read the source file.
reader = vtkXMLImageDataReader()
reader.SetFileName(file_name)
# Read the source file.
reader = vtk.vtkXMLUnstructuredGridReader()
reader.SetFileName("tetra.vtu")
reader.Update() # Needed because of GetScalarRange
reader = vtkXMLPolyDataReader()
reader.SetFileName("tetra.vtp")
reader.Update()
# vtkSLCReader to read.
reader = vtkSLCReader()
reader.SetFileName(InputFilename)
reader.Update()
# Read Exodus Data
reader = vtkExodusIIReader()
reader.SetFileName(filename)
reader.UpdateInformation()
reader.SetTimeStep(10)
reader.SetAllArrayStatus(vtkExodusIIReader.NODAL, 1) # enables all NODAL variables
reader.Update()
# Read and display for verification
reader = vtkPLYReader()
reader.SetFileName(filename)
reader.Update()
# HDRReader
# noinspection PyUnresolvedReferences
import vtkmodules.vtkRenderingOpenGL2
from vtkmodules.vtkIOImage import vtkHDRReader
from vtkmodules.vtkInteractionImage import vtkImageViewer
from vtkmodules.vtkRenderingCore import vtkRenderWindowInteractor
reader = vtkHDRReader()
# Check the image can be read
if not reader.CanReadFile(file_name):
print('CanReadFile failed for ', file_name)
return
reader.SetFileName(file_name)
reader.UpdateInformation()
#!/usr/bin/env python
# noinspection PyUnresolvedReferences
import vtkmodules.vtkInteractionStyle
# noinspection PyUnresolvedReferences
import vtkmodules.vtkRenderingOpenGL2
from vtkmodules.vtkCommonColor import vtkNamedColors
from vtkmodules.vtkIOImport import vtk3DSImporter
from vtkmodules.vtkRenderingCore import (
vtkCamera,
vtkRenderWindow,
vtkRenderWindowInteractor,
vtkRenderer
)
#E:/space-ship.3DS
def main():
fileName = "E:/space-ship.3DS" #get_program_parameters()
importer = vtk3DSImporter()
importer.SetFileName(fileName)
importer.ComputeNormalsOn()
colors = vtkNamedColors()
renderer = vtkRenderer()
renWin = vtkRenderWindow()
iren = vtkRenderWindowInteractor()
renWin.AddRenderer(renderer)
renderer.SetBackground2(colors.GetColor3d('Gold'))
renderer.SetBackground(colors.GetColor3d('Wheat'))
renderer.GradientBackgroundOn()
iren.SetRenderWindow(renWin)
importer.SetRenderWindow(renWin)
importer.Update()
# actors = vtkActorCollection()
actors = renderer.GetActors()
print('There are', actors.GetNumberOfItems(), 'actors.')
renWin.SetWindowName('3DSImporter')
renWin.Render()
camera = vtkCamera()
camera.SetPosition(0, -1, 0)
camera.SetFocalPoint(0, 0, 0)
camera.SetViewUp(0, 0, 1)
camera.Azimuth(150)
camera.Elevation(30)
renderer.SetActiveCamera(camera)
renderer.ResetCamera()
renderer.ResetCameraClippingRange()
renWin.Render()
iren.Start()
def get_program_parameters():
import argparse
description = 'Importing a 3ds file.'
epilogue = '''
'''
parser = argparse.ArgumentParser(description=description, epilog=epilogue,
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('filename', help='iflamingo.3ds.')
args = parser.parse_args()
return args.filename
if __name__ == '__main__':
main()
Part4 Writer
#!/usr/bin/env python
# noinspection PyUnresolvedReferences
import vtkmodules.vtkInteractionStyle
# noinspection PyUnresolvedReferences
import vtkmodules.vtkRenderingOpenGL2
from vtkmodules.vtkCommonColor import vtkNamedColors
from vtkmodules.vtkFiltersSources import vtkSphereSource
from vtkmodules.vtkIOGeometry import (
vtkSTLReader,
vtkSTLWriter
)
sphereSource = vtkSphereSource()
sphereSource.Update()
# Write the stl file to disk
stlWriter = vtkSTLWriter()
stlWriter.SetFileName(filename)
stlWriter.SetInputConnection(sphereSource.GetOutputPort())
stlWriter.Write()
# WritePLY
# noinspection PyUnresolvedReferences
import vtkmodules.vtkInteractionStyle
# noinspection PyUnresolvedReferences
import vtkmodules.vtkRenderingOpenGL2
from vtkmodules.vtkCommonColor import vtkNamedColors
from vtkmodules.vtkFiltersSources import vtkSphereSource
from vtkmodules.vtkIOPLY import (
vtkPLYReader,
vtkPLYWriter
)
sphereSource = vtkSphereSource()
sphereSource.Update()
plyWriter = vtkPLYWriter()
plyWriter.SetFileName(filename)
plyWriter.SetInputConnection(sphereSource.GetOutputPort())
plyWriter.Write()
# ImageWriter
#!/usr/bin/env python
# noinspection PyUnresolvedReferences
import vtkmodules.vtkInteractionStyle
# noinspection PyUnresolvedReferences
import vtkmodules.vtkRenderingOpenGL2
from vtkmodules.vtkCommonColor import vtkNamedColors
from vtkmodules.vtkFiltersSources import vtkSphereSource
from vtkmodules.vtkIOImage import (
vtkBMPWriter,
vtkJPEGWriter,
vtkPNGWriter,
vtkPNMWriter,
vtkPostScriptWriter,
vtkTIFFWriter
)
from vtkmodules.vtkRenderingCore import (
vtkActor,
vtkPolyDataMapper,
vtkRenderWindow,
vtkRenderWindowInteractor,
vtkRenderer,
vtkWindowToImageFilter
)
def main():
colors = vtkNamedColors()
# Set the background color.
colors.SetColor('BkgColor', [26, 51, 102, 255])
# create a rendering window and renderer
ren = vtkRenderer()
renWin = vtkRenderWindow()
renWin.AddRenderer(ren)
# create a renderwindowinteractor
iren = vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
# create source
source = vtkSphereSource()
source.SetCenter(0, 0, 0)
source.SetRadius(5.0)
# mapper
mapper = vtkPolyDataMapper()
mapper.SetInputConnection(source.GetOutputPort())
# actor
actor = vtkActor()
actor.SetMapper(mapper)
# color the actor
actor.GetProperty().SetColor(colors.GetColor3d('Yellow'))
# assign actor to the renderer
ren.AddActor(actor)
ren.SetBackground(colors.GetColor3d('BkgColor'))
renWin.SetWindowName('ImageWriter')
renWin.Render()
ext = ['', '.png', '.jpg', '.ps', '.tiff', '.bmp', '.pnm']
filenames = list(map(lambda x: 'ImageWriter' + x, ext))
filenames[0] = filenames[0] + '1'
for f in filenames:
WriteImage(f, renWin, rgba=False)
iren.Initialize()
iren.Start()
def WriteImage(fileName, renWin, rgba=True):
'''
Write the render window view to an image file.
Image types supported are:
BMP, JPEG, PNM, PNG, PostScript, TIFF.
The default parameters are used for all writers, change as needed.
:param fileName: The file name, if no extension then PNG is assumed.
:param renWin: The render window.
:param rgba: Used to set the buffer type.
:return:
'''
import os
if fileName:
# Select the writer to use.
path, ext = os.path.splitext(fileName)
ext = ext.lower()
if not ext:
ext = '.png'
fileName = fileName + ext
if ext == '.bmp':
writer = vtkBMPWriter()
elif ext == '.jpg':
writer = vtkJPEGWriter()
elif ext == '.pnm':
writer = vtkPNMWriter()
elif ext == '.ps':
if rgba:
rgba = False
writer = vtkPostScriptWriter()
elif ext == '.tiff':
writer = vtkTIFFWriter()
else:
writer = vtkPNGWriter()
windowto_image_filter = vtkWindowToImageFilter()
windowto_image_filter.SetInput(renWin)
windowto_image_filter.SetScale(1) # image quality
if rgba:
windowto_image_filter.SetInputBufferTypeToRGBA()
else:
windowto_image_filter.SetInputBufferTypeToRGB()
# Read from the front buffer.
windowto_image_filter.ReadFrontBufferOff()
windowto_image_filter.Update()
writer.SetFileName(fileName)
writer.SetInputConnection(windowto_image_filter.GetOutputPort())
writer.Write()
else:
raise RuntimeError('Need a filename.')
if __name__ == '__main__':
main()
Part 5 show vtk
#!/usr/bin/env python
# noinspection PyUnresolvedReferences
import vtkmodules.vtkInteractionStyle
# noinspection PyUnresolvedReferences
import vtkmodules.vtkRenderingOpenGL2
from vtkmodules.vtkCommonColor import vtkNamedColors
from vtkmodules.vtkFiltersSources import vtkSphereSource
from vtkmodules.vtkIOGeometry import (
vtkSTLReader,
vtkSTLWriter
)
from vtkmodules.vtkRenderingCore import (
vtkActor,
vtkPolyDataMapper,
vtkRenderWindow,
vtkRenderWindowInteractor,
vtkRenderer
)
def get_program_parameters():
import argparse
description = 'Generate image data, then write a .stl file.'
epilogue = '''
'''
parser = argparse.ArgumentParser(description=description, epilog=epilogue)
parser.add_argument('filename', help='A required stl filename.', nargs='?',
const='TestWriteSTL.ply',
type=str, default='TestWriteSTL.ply')
args = parser.parse_args()
return args.filename
def main():
colors = vtkNamedColors()
filename = get_program_parameters()
sphereSource = vtkSphereSource()
sphereSource.Update()
# Write the stl file to disk
stlWriter = vtkSTLWriter()
stlWriter.SetFileName(filename)
stlWriter.SetInputConnection(sphereSource.GetOutputPort())
stlWriter.Write()
# Read and display for verification
reader = vtkSTLReader()
reader.SetFileName(filename)
mapper = vtkPolyDataMapper()
mapper.SetInputConnection(reader.GetOutputPort())
actor = vtkActor()
actor.SetMapper(mapper)
actor.GetProperty().SetColor(colors.GetColor3d('MistyRose'))
# Create a rendering window and renderer
ren = vtkRenderer()
renWin = vtkRenderWindow()
renWin.AddRenderer(ren)
renWin.SetWindowName('WriteSTL')
# Create a renderwindowinteractor
iren = vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
# Assign actor to the renderer
ren.AddActor(actor)
ren.SetBackground(colors.GetColor3d('cobalt_green'))
# Enable user interface interactor
iren.Initialize()
renWin.Render()
iren.Start()
if __name__ == '__main__':
main()
效果: