python animation 保存gif/mp4失败

在尝试使用Python的FuncAnimation接口创建动图时,遇到错误‘OSError: Error saving animation to file (cause: [Errno 22] Invalid argument)’。解决方案包括安装magick和ffmpeg。通过指定ffmpeg和convert的路径,成功保存为gif和mp4格式。博客中详细介绍了安装和配置过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题描述

在参阅了几位优秀博主关于python animation的介绍后,我复制了详解如何使用Python绘制动图(一)|解析FuncAnimation接口用法以及实践.的代码,结果动图变成了静图,还报错说
“OSError: Error saving animation to file (cause: [Errno 22] Invalid argument) Stdout: b’’ StdError: b’’. It may help to re-run with --verbose-debug”

// 
OSError: Error saving animation to file (cause: [Errno 22] Invalid argument) Stdout: b'' StdError: b''. It may help to re-run with --verbose-debug.

后来看到另一位博主讲,想要保存gif格式需要安装magick
保存mp4文件则需要ffmpeg
于是开始安装

解决方法

步骤一:安装

安装magick
附下载链接.环境变量不需配置(人家自动也会有配置),后面会有介绍
安装ffmpeg
附下载链接.这个只需要下载,然后解压到一个你喜欢的文件夹就可以了

步骤二:配置环境

其实也谈不上配置环境,无非就是在代码前面加上两个路径

//
import numpy as np
import matplotlib.animation as animation 
import matplotlib.pyplot as plt
plt.rcParams['animation.ffmpeg_path'] = r'C:\Users\ydm\Anaconda3\ffmpeg\bin\ffmpeg.exe'
plt.rcParams['animation.convert_path'] = r'C:\Program Files\ImageMagick-7.0.10-Q16-HDRI\magick.exe'

步骤三:画图

#gif格式
Writer = animation.ImageMagickFileWriter()
ani.save('h.gif', writer = Writer)
#mp4格式
#Writer = animation.FFMpegWriter(fps=20, metadata=dict(artist='Me'))
#ani.save('h.mp4', writer = Writer)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值