如何解决通过 PIL 将图像转换为动画 webp 文件时出错
我尝试过的两种方法:
- 将 PIL.Images 列表保存为 webp 文件:
from PIL import Image
import os
frames = []
fdir = "./frames"
for f in sorted(os.listdir(fdir)):
f = os.path.join(fdir,f)
frames.append(Image.open(f))
frames = frames[:100]
frames[0].save("./test.webp","webp",save_all=True,append_images=frames[1:])
我得到了错误:
Traceback (most recent call last):
File "/Users/feiyangsuo/anaconda3/envs/video/lib/python3.8/site-packages/IPython/core/interactiveshell.py",line 3441,in run_code
exec(code_obj,self.user_global_ns,self.user_ns)
File "<ipython-input-11-e3361ee93efc>",line 1,in <module>
frames[0].save("./test.webp",append_images=frames[1:])
File "/Users/feiyangsuo/anaconda3/envs/video/lib/python3.8/site-packages/PIL/Image.py",line 2172,in save
save_handler(self,fp,filename)
File "/Users/feiyangsuo/anaconda3/envs/video/lib/python3.8/site-packages/PIL/WebPImagePlugin.py",line 292,in _save_all
timestamp += duration
TypeError: unsupported operand type(s) for +=: 'int' and 'NoneType'
- 将 gif 文件保存到 webp:
frames[0].save("./test.gif","gif",append_images=frames[1:]) # this works fine
jb = Image.open("./test.gif")
jb.save("./test.webp",save_all=True)
我遇到了和上面一样的错误。
我使用的是 PIL 8.2.0。错误似乎来自PIL的基本实现,所以我不知道如何修改我的代码。
解决方法
问题已通过在 duration
函数中添加 save
参数解决。
例如
jb = Image.open("./test.gif")
jb.save("./test.webp","webp",save_all=True,duration=10)
对于 .gif 这个参数可能是默认值,因此不需要手动设置。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。