2017-10-12 37 views
3

我一直在试图在我的windows电脑上导出一个动画gif好几天。使用matplotlib导出动画gif

以下是基本代码:

import numpy as np 
import matplotlib.pyplot as plt 
from matplotlib.animation import FuncAnimation 

fig, ax = plt.subplots() 
xdata, ydata = [], [] 
ln, = plt.plot([], [], 'ro', animated=True) 

def init(): 
    ax.set_xlim(0, 2*np.pi) 
    ax.set_ylim(-1, 1) 
    return ln, 

def update(frame): 
    xdata.append(frame) 
    ydata.append(np.sin(frame)) 
    ln.set_data(xdata, ydata) 
    return ln, 

ani = FuncAnimation(fig, update, frames=np.linspace(0, 2*np.pi, 128), 
        init_func=init, blit=True) 
ani.save("test.gif") 
plt.show() 

和错误:

>>> python .\anim_test.py 
Traceback (most recent call last): 
    File ".\anim_test.py", line 22, in <module> 
    ani.save("test.gif") 
    File "C:\Users\ishma\Anaconda2\lib\site-packages\matplotlib\animation.py", line 1063, in save 
    writer.grab_frame(**savefig_kwargs) 
    File "C:\Users\ishma\Anaconda2\lib\site-packages\matplotlib\animation.py", line 336, in grab_frame 
    'with --verbose-debug.'.format(e, out, err)) 
IOError: Error saving animation to file (cause: [Errno 22] Invalid argument) Stdout: StdError: . It may help to re-run 
with --verbose-debug. 
PS C:\Users\ishma\Dropbox (SteinLab)\spectra\MassSpecPlot> python .\anim_test.py --verbose-debug 
$HOME=C:\Users\ishma 
matplotlib data path C:\Users\ishma\Anaconda2\lib\site-packages\matplotlib\mpl-data 

***************************************************************** 
You have the following UNSUPPORTED LaTeX preamble customizations: 

Please do not ask for support with these customizations active. 
***************************************************************** 

loaded rc file C:\Users\ishma\Anaconda2\lib\site-packages\matplotlib\mpl-data\matplotlibrc 
matplotlib version 2.0.2 
verbose.level debug 
interactive is False 
platform is win32 
loaded modules: <dictionary-keyiterator object at 0x0000000003EA0048> 
CACHEDIR=C:\Users\ishma\.matplotlib 
Using fontManager instance from C:\Users\ishma\.matplotlib\fontList.cache 
backend Qt5Agg version 5.6 
Animation.save using <class 'matplotlib.animation.FFMpegWriter'> 
frame size in pixels is 640 x 480 
MovieWriter.run: running command: ffmpeg -f rawvideo -vcodec rawvideo -s 640x480 -pix_fmt rgba -r 5.0 -i pipe: -vcodec h 
264 -pix_fmt yuv420p -y test.gif 
MovieWriter.grab_frame: Grabbing frame. 
findfont: Matching :family=sans-serif:style=normal:variant=normal:weight=400:stretch=normal:size=10.0 to DejaVu Sans (u' 
C:\\Users\\ishma\\Anaconda2\\lib\\site-packages\\matplotlib\\mpl-data\\fonts\\ttf\\DejaVuSans.ttf') with score of 0.0000 
00 
MovieWriter.grab_frame: Grabbing frame. 
MovieWriter -- Error running proc: 
None 
None 
MovieWriter -- Command stdout: 
None 
MovieWriter -- Command stderr: 
None 
Traceback (most recent call last): 
    File ".\anim_test.py", line 22, in <module> 
    ani.save("test.gif") 
    File "C:\Users\ishma\Anaconda2\lib\site-packages\matplotlib\animation.py", line 1063, in save 
    writer.grab_frame(**savefig_kwargs) 
    File "C:\Users\ishma\Anaconda2\lib\site-packages\matplotlib\animation.py", line 336, in grab_frame 
    'with --verbose-debug.'.format(e, out, err)) 
IOError: Error saving animation to file (cause: [Errno 22] Invalid argument) Stdout: None StdError: None. It may help to 
re-run with --verbose-debug. 

如果我切换输出文件类型为MP4,代码工作。在从有错误产生视频文件的人那里读取很多线程后,我觉得我已经尝试了不同作者的各种组合(包括FFMpegWriter,ImageMagickWriter,AVConvWriter和FileWriters),确保相关程序在我的PATH中,更改了设置在matplotlibrc中,并尝试了多台电脑。我很难过。

我发现只有一个线程引用这个确切的错误: https://stackoverflow.com/questions/46562938/oserror-errno-22-invalid-argument-error-saving-animation-to-file

但是,随着在那里的意见建议并没有解决我的问题。

任何想法?谢谢你的帮助。

回答

2

从问题的代码,你基本上要求使用MPEG编写器生成动画gif。但MPEG编写器只能制作视频。

通过matplotlib动画模块生成动画gif的标准方法是使用ImageMagick。

所以首先改变你的行

ani.save("test.gif",writer="imagemagick") 

现在这个工作的rcParam animation.convert_path必须指向ImageMagick的convert程序。
看来你在这里的窗口,所以最好包括完整的路径。因此,在保存之前,设置

plt.rcParams["animation.convert_path"] = "C:\ProgramFiles\ImageMagick\convert.exe" 

或任何您的路径convert.exe可能是。

现在可能很明显convert.exe不再是更新版本的imageMagick的一部分了。 The documentation

If these tools are not available, you can simply append them to the magick tool like this:
magick convert .

对于matplotlib动画这意味着你将需要设置一个额外的参数。设置路径

plt.rcParams["animation.convert_path"] = "C:\ProgramFiles\ImageMagick\magick.exe" 

然后调用

ani.save("test.gif",writer="imagemagick", extra_args="convert") 
+0

我安装的ImageMagick的似乎并不在它CONVERT.EXE。应该是? 虽然我在ImageMagick目录中看到了ffmpeg.exe。 – ishmandoo

+0

如果您正在使用ImageMagick 7,则转换已重命名为magick。那么你有magick.exe吗? – fmw42

+0

@ fmw42我确实有'ImageMagick-7.0.3-Q16',我有一个'convert.exe'。我也有一个'magick.exe',但在这种情况下没有用。您是否有任何关于重命名的声明来源?否则我不会相信这一点。 – ImportanceOfBeingErnest