【问题标题】:Saving a mayavi animation保存 mayavi 动画
【发布时间】:2014-07-25 14:58:26
【问题描述】:

我目前正在尝试保存我的模拟生成的 mayavi 动画,因此我不必每次都重新运行代码来查看它。

plt = points3d(x_coord, y_coord, z_coord)

msplt = plt.mlab_source
@mlab.animate(delay=100)
def anim():
    f = mlab.gcf()
    while True:
        #animation updates here
        msplt.set(x = x_coord, y = y_coord, z = z_coord)

        yield


anim()
mlab.savefig(filename = 'ani.mp4')
mlab.show()

我尝试通过管道编辑器保存它,只是得到它所在的帧的静止状态,而 mlab.savefig 不会生成文件。任何帮助表示赞赏。

【问题讨论】:

  • 很有创意,使用mlab.savefig()尝试导出为.mp4。您可能会猜到,VTK 不是这样工作的。您可以改为使用屏幕截图制作电影。例如,在 X11 系统上,这可以通过 ffmpeg -x11grab 完成
  • 有具体的代码示例吗?
  • 我在依赖 ffmpeg 的神经影像可视化程序中写了一些类似的东西(尽管没有经过持续使用的审查)。查看github.com/aestrivex/cvu/blob/master/cvu/dataview.py 中的make_movie 函数。这不像“给我代码让它工作”那么简单。没关系,因为这很棘手,而且您的问题并不是关于 mayavi,因为 mayavi 不支持这一点。

标签: python animation mayavi


【解决方案1】:

以下方法适用于查看动画,将每一帧保存为“png”,然后将它们转换为电影,但在这种情况下,放弃播放动画可能是最快的,只需循环浏览数据保存数字,然后用这种方法制作视频。

from mayavi import mlab
import numpy as np
import os

# Output path for you animation images
out_path = './'
out_path = os.path.abspath(out_path)
fps = 20
prefix = 'ani'
ext = '.png'

# Produce some nice data.
n_mer, n_long = 6, 11
pi = np.pi
dphi = pi/1000.0
phi = np.arange(0.0, 2*pi + 0.5*dphi, dphi, 'd')
mu = phi*n_mer
x = np.cos(mu)*(1+np.cos(n_long*mu/n_mer)*0.5)
y = np.sin(mu)*(1+np.cos(n_long*mu/n_mer)*0.5)
z = np.sin(n_long*mu/n_mer)*0.5

# Init plot
plt = mlab.points3d(x[0], y[0], z[0])

padding = len(str(len(x)))

# Define data source and update routine
msplt = plt.mlab_source
@mlab.animate(delay=10)
def anim():
    f = mlab.gcf()
    for i in range(len(x)):
        #animation updates here
        msplt.set(x=x[i], y=y[i], z=z[i])

        # create zeros for padding index positions for organization
        zeros = '0'*(padding - len(str(i)))

        # concate filename with zero padded index number as suffix
        filename = os.path.join(out_path, '{}_{}{}{}'.format(prefix, zeros, i, ext))

        mlab.savefig(filename=filename)

        yield

anim()
mlab.view(distance=15)
mlab.show()

import subprocess
ffmpeg_fname = os.path.join(out_path, '{}_%0{}d{}'.format(prefix, padding, ext))
cmd = 'ffmpeg -f image2 -r {} -i {} -vcodec mpeg4 -y {}.mp4'.format(fps,
                                                                    ffmpeg_fname,
                                                                    prefix)
print cmd
subprocess.check_output(['bash','-c', cmd])

# Remove temp image files with extension
[os.remove(f) for f in os.listdir(out_path) if f.endswith(ext)]

【讨论】:

    猜你喜欢
    • 2012-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-03
    • 2013-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多