我曾尝试过两种方法:
- 将一系列的PIL图片保存为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", "webp", save_all=True, 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", save_all=True, append_images=frames[1:]) # this works fine
jb = Image.open("./test.gif")
jb.save("./test.webp", "webp", save_all=True)
我也遇到了与上述相同的错误。
我正在使用PIL 8.2.0版本。这个错误似乎是由于PIL基础实现引起的,因此我不知道如何修改我的代码。
frames.append( Image.open( f, formats=None ) )
。 - Derek