早上好,我想用pvpython在VTK文件中做一个3D体积的切片,然后我想保存这个图像(可能用一个以白色为中心的对称调色板)。
具体地说,我想用python制作下面用Paraview拍摄的图像:

我是pvpython或python的新手,所以我报告了我的第一个脚本,但它不工作:
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from scipy.interpolate import griddata
from paraview import simple as pvs
import numpy as np
import vtk
import matplotlib
from vtk.util.numpy_support import vtk_to_numpy
reader = vtk.vtkDataSetReader()
reader.SetFileName("../deltau-deltau.vtk")
reader.ReadAllScalarsOn()
reader.Update()
plane = vtk.vtkPlane()
plane.SetOrigin(16, 0, 0)
plane.SetNormal(1, 0, 0)
cutter = vtk.vtkCutter()
cutter.SetCutFunction(plane)
cutter.SetInputConnection(reader.GetOutputPort())
cutter.Update()
data = cutter.GetOutput()
# Coordinates of nodes in the mesh
nodes_vtk_array= reader.GetOutput().GetPoints().GetData()
#Get the coordinates of the nodes and their variables
nodes_nummpy_array = vtk_to_numpy(nodes_vtk_array)
x=nodes_nummpy_array[:,0]
y=nodes_nummpy_array[:,1]
z=nodes_nummpy_array[:,2]
variable_numpy_array = vtk_to_numpy(data)
var = variable_numpy_array
# Figure bounds
npts = 100
xmin, xmax = min(x), max(x)
ymin, ymax = min(y), max(y)
zmin, zmax = min(z), max(z)
# Grid
xi = np.linspace(xmin, xmax, npts)
yi = np.linspace(ymin, ymax, npts)
zi = np.linspace(zmin, zmax, npts)
# The mesh is unstructured, so
var_plot = griddata((z, y), var, (zi[None,:], yi[:,None]), method='cubic')
plt.plot(zi, yi, var_plot)
#plt.colorbar()
plt.savefig("figure.png")发布于 2021-09-06 09:22:41
首先,请注意ParaView脚本和VTK脚本之间的区别。您提供的脚本似乎是纯VTK脚本。ParaView脚本比VTK更高级,也更像python。
如果您可以在ParaView中实现处理,我建议您只使用ParaView脚本工具,而不要使用VTK脚本(例如,不要使用import vtk,不要创建任何vtkXXX()对象)
在ParaView中,您可以像解释here一样跟踪操作以生成python脚本。这对于理解特定操作的python对应物很有用。如果要编写完整分析的脚本,还可以保存python state。
本文档还提供了从GUI和python进行处理和分析的示例。
如果你仍然喜欢VTK脚本,请改写你的帖子,并解释什么是“不工作”。
https://stackoverflow.com/questions/69062937
复制相似问题