anibali / pywebp

Python bindings for WebP
MIT License
74 stars 24 forks source link

webp.WebPError: unsupported image mode: P #17

Closed Directory closed 4 years ago

Directory commented 4 years ago

I have a simple gif. I split the gif into each frame in a list. Each frame being an image object opned ans saved as a png. The same error happens when saved as BMP however its slightly different for JPEG.

from PIL import Image
import webp
from io import BytesIO

img = Image.open('test.gif')
frames = []
pframes = []

for seek_point in range(img.n_frames):
    img.seek(seek_point)
    b = BytesIO()
    img.save(b, format='PNG')
    frames.append(b)

for f in frames:
    pframes.append(Image.open(f))

webp.save_images(pframes, 'test.webp', fps=24, lossless=True)
anibali commented 4 years ago

Please supply test.gif

Directory commented 4 years ago

its this. https://lukeroberts.tv/blog/wp-content/uploads/2019/05/gif1.gif. just downloaded and renamed.

anibali commented 4 years ago

OK thanks, I'll take a look when I get a chance.

Directory commented 4 years ago

additionally i encountered the exact same error but different traceback when using the advanced api.

the traceback for save_images is

Traceback (most recent call last):
  File "D:\Desktop\test.py", line 29, in <module>
    webp.save_images(pframes, 'test.webp', fps=24, lossless=True)
  File "D:\Program Files\Python38\lib\site-packages\webp\__init__.py", line 590, in save_images
    return mimwrite(file_path, arrs, **kwargs, pilmode=imgs[0].mode)
  File "D:\Program Files\Python38\lib\site-packages\webp\__init__.py", line 494, in mimwrite
    pics = [WebPPicture.from_numpy(arr, pilmode=pilmode) for arr in arrs]
  File "D:\Program Files\Python38\lib\site-packages\webp\__init__.py", line 494, in <listcomp>
    pics = [WebPPicture.from_numpy(arr, pilmode=pilmode) for arr in arrs]
  File "D:\Program Files\Python38\lib\site-packages\webp\__init__.py", line 223, in from_numpy
    raise WebPError('unsupported image mode: ' + pilmode)
webp.WebPError: unsupported image mode: P

however the traceback for the advanced api is

Traceback (most recent call last):
  File "D:\Desktop\test.py", line 32, in <module>
    pic = webp.WebPPicture.from_pil(img)
  File "D:\Program Files\Python38\lib\site-packages\webp\__init__.py", line 235, in from_pil
    return WebPPicture.from_numpy(np.asarray(img), pilmode=img.mode)
  File "D:\Program Files\Python38\lib\site-packages\webp\__init__.py", line 223, in from_numpy
    raise WebPError('unsupported image mode: ' + pilmode)
webp.WebPError: unsupported image mode: P

im on windows 10 and py3.8. Ive encountered the same error on ubuntu server 20.04. ive also tested it on this gif https://compote.slate.com/images/697b023b-64a5-49a0-8059-27b963453fb1.gif and the same error occurs on both systems.

anibali commented 4 years ago

If you want to try something out in the meantime, try converting the PIL images to RGB mode after you open them:

pframes.append(Image.open(f).convert('RGB'))

Use RGBA if you need transparency.

Directory commented 4 years ago

Oh wow that actually did work. Thanks. It should probably be specified in the documentation or internally automatically convert it but for my use this works. Thanks.

anibali commented 4 years ago

Automatic conversion added in dfea4995696ce7091ecc8b0cd9446d31211d1abd.