Flicic
Flicic

Reputation: 119

Error when converting images to animated webp file by PIL

Two ways I've tried:

  1. Save a list of PIL.Images as a webp file:
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:])

and I got error:

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'
  1. Save a gif file to 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)

and I got the same error as above.

I'm using PIL 8.2.0. The error seems from the basic implementation of PIL, so I have no idea how to modify my code.

Upvotes: 3

Views: 1248

Answers (1)

Flicic
Flicic

Reputation: 119

The problem is fixed by adding duration parameter to save function.

e.g.

jb = Image.open("./test.gif")
jb.save("./test.webp", "webp", save_all=True, duration=10)

For .gif this parameter maybe in default value thus unnecessary to be set manually.

Upvotes: 1

Related Questions