Python VTK中文资料好少啊,有个任务要调整摄像机位置并且保存图片,中文帖子没几个这方面的,啃官方文档解决啦,粗略记录一下。 调整相机:
render = vtk.vtkRenderer()
render.AddActor(actor)
render.SetBackground(0, 0, 0)
camera = render.GetActiveCamera()
camera.SetPosition(0, 60, 500)
camera.SetFocalPoint(0, 100, 0)
camera.Azimuth(60)
camera.Elevation(-300)
camera.Dolly(1.5)
camera.Roll(11)
camera.Pitch(11)
camera.Zoom(10)
保存图片:
from pathlib import Path
from vtkmodules.vtkIOImage import (
vtkBMPWriter,
vtkJPEGWriter,
vtkPNGWriter,
vtkPNMWriter,
vtkPostScriptWriter,
vtkTIFFWriter
)
from vtkmodules.vtkRenderingCore import vtkWindowToImageFilter
def write_image(file_name, ren_win, 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 file_name: The file name, if no extension then PNG is assumed.
:param ren_win: The render window.
:param rgba: Used to set the buffer type.
:return:
"""
if file_name:
valid_suffixes = ['.bmp', '.jpg', '.png', '.pnm', '.ps', '.tiff']
parent = Path(file_name).resolve().parent
path = Path(parent) / file_name
if path.suffix:
ext = path.suffix.lower()
else:
ext = '.png'
path = Path(str(path)).with_suffix(ext)
if path.suffix not in valid_suffixes:
print(f'No writer for this file suffix: {ext}')
return
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(ren_win)
windowto_image_filter.SetScale(1)
if rgba:
windowto_image_filter.SetInputBufferTypeToRGBA()
else:
windowto_image_filter.SetInputBufferTypeToRGB()
windowto_image_filter.ReadFrontBufferOff()
windowto_image_filter.Update()
writer.SetFileName(path)
writer.SetInputConnection(windowto_image_filter.GetOutputPort())
writer.Write()
else:
raise RuntimeError('Need a filename.')
详细看官方文档 官方文档链接:https://kitware.github.io/vtk-examples/site/
|