我正在尝试找出如何将使用Mayavi创建的3D图形导出为电影,以便我可以在Powerpoint等演示中使用。我发现了一个使用moviepy进行此操作的讨论,网址为
http://zulko.github.io/blog/2014/11/29/data-animations-with-python-and-moviepy/
我使用了这段代码,并进行了如下轻微修改
duration = 6
def make_frame(t):
u = np.linspace(0,2*np.pi,360)
y = np.sin(3*u)*(0.2+0.5*np.cos(2*np.pi*t/duration))
pore_surface.mlab_source.set(y = y)
mlab.view(azimuth= 360*t/duration, distance=200)
.
.
.
verts, faces = marching_cubes(large_region, 0.5, (1., 1., 1.))
surface_area = mesh_surface_area(verts, faces)
pore_surface = mlab.triangular_mesh([vert[0] for vert in verts],[vert[1] for vert in verts],[vert[2] for vert in verts],faces)
mlab.show(pore_surface)
animation = mpy.VideoClip(make_frame, duration=duration).resize(0.5)
animation.write_videofile("pore_surface.mp4", fps=20)
animation.write_gif("pore_surface.gif", fps=20)
其中 marching_cubes 是来自 scikits image 库。
然而,我按照以下方式执行时出现了广播错误(每个新值中有 360 个值)。
在对象的 traits 通知处理程序中发生异常:对象为:,特征为:y,旧值为:[0.0, 0.0, 1.0, ..., 62.5, 63.0, 63.0],新值为:[0.00000000e+00, 3.67371235e-02, 7.33729915e-02, 1.09806628e-01,1.45937613e-01,1.81666362e-01,2.16894399e-01,2.51524628e-01,...]
回溯(最近的调用最先): 文件“/Users/iz9/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/traits/trait_notifiers.py”,第340行,在call中: self.handler(*args) 文件“/Users/iz9/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/mayavi/tools/sources.py”,第835行,在_y_changed中: self.points [:,1] = y.ravel() ValueError:无法将形状为(360)的输入数组广播到形状为(43505)的形状 ERROR:traits:对象的特征通知处理程序发生异常:,特征:y,旧值:[0. 0. 1. ...,新值:[0.00000000e + 00 3.67371235e-02 7.33729915e-02 1.09806628e-01 1.45937613e-01 1.81666362e-01 2.16894399e-01 2.51524628e-01 。 。 。 这个错误重复很多次。与此同时,Mayavi场景确实显示,并且在此错误继续时它确实显示图像旋转
我在Mac上使用Enthought Canopy运行Python。