2017-10-12 2 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() 

그리고 오류 : 나는 MP4로 출력 파일 형식을 전환하면

>>> 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. 

는, 코드가 작동

여기에 기본 코드입니다. 비디오 파일을 생성하는 오류가있는 사람들로부터 많은 스레드를 읽은 후에는 다른 작성자 (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을 사용하는 것입니다. 이것이 ImageMagick과의 convert 프로그램을 가리해야 rcParam animation.convert_path을 작동하려면

그래서 처음으로 지금

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

에 라인을 변경합니다.
여기에는 Windows에있는 것처럼 보입니다. 따라서 전체 경로를 포함하는 것이 가장 좋습니다. 따라서 저장하기 전에,

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

또는 convert.exe로 경로가있을 수 있습니다 무엇을 설정합니다.

이제는 convert.exe이 더 이상 imageMagick의 최신 버전에 속하지 않을 수도 있습니다. The documentation이 당신이 여분의 인수를 설정해야한다는 의미하기 matplotlib 애니메이션

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

을 말한다. 다음에 Convert.exe를이하지 않는 것 ImageMagick과의 설치 내

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

전화

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

의 경로를 설정합니다. 그럴까요? 그래도 ImageMagick 디렉토리에 ffmpeg.exe가 있습니다. – ishmandoo

+0

ImageMagick 7을 사용하는 경우 convert가 magick으로 이름이 변경되었습니다. 그럼 magick.exe가 있습니까? – fmw42

+0

@ fmw42'ImageMagick-7.0.3-Q16'이 있는데'convert.exe'가 있습니다. 또한 magick.exe도 있지만이 경우에는 유용하지 않습니다. 이름 바꾸기에 대한 귀하의 주장에 대한 근원이 있습니까? 그렇지 않으면 나는 그것을 신뢰하지 않을 것이다. – ImportanceOfBeingErnest