我一直在試圖在我的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
但是,隨着在那裏的意見建議並沒有解決我的問題。
任何想法?謝謝你的幫助。
我安裝的ImageMagick的似乎並不在它CONVERT.EXE。應該是? 雖然我在ImageMagick目錄中看到了ffmpeg.exe。 – ishmandoo
如果您正在使用ImageMagick 7,則轉換已重命名爲magick。那麼你有magick.exe嗎? – fmw42
@ fmw42我確實有'ImageMagick-7.0.3-Q16',我有一個'convert.exe'。我也有一個'magick.exe',但在這種情況下沒有用。您是否有任何關於重命名的聲明來源?否則我不會相信這一點。 – ImportanceOfBeingErnest