anibali / pywebp

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

"WebPError: failed to create decoder" when running WebPAnimDecoder.new() #55

Closed prof-m closed 11 months ago

prof-m commented 11 months ago

Context

I recently started trying to use the pywebp library to help with a Python script I'm making to convert animated webps to either gifs or mp4s. My first step in converting animated webps into mp4s is to extract the individual frames from the webp file.

Platform: Windows 11 64-bit Python version: 3.11 Using poetry for dependency management

Issue

Following the code example from the README.md, I have the following code for extracting the frames from the animated webp.

with open(webp_file_path, "rb") as file_buffer:
        webp_data = webp.WebPData.from_buffer(file_buffer.read())
        decoder = webp.WebPAnimDecoder.new(webp_data, webp.WebPAnimDecoderOptions.new()) #Exception thrown here
        for i, frame, timestamp_ms in enumerate(decoder.frames()):
            total_duration_ms += timestamp_ms
            frame_path = Path(tmp_frames_subdir_path, f"frame_{i:05d}.png")
            frame_img = Image.fromarray(frame)
            frame_img.save(frame_path)

On the third line, where I try to make the decoder, the following WebPError occurs: WebPError: failed to create decoder

The stacktrace points to this line of code being where the error gets thrown from within the library:

│   493 │   │   │   dec_opts = WebPAnimDecoderOptions.new()                   │
│   494 │   │   ptr = lib.WebPAnimDecoderNew(webp_data.ptr, dec_opts.ptr)     │
│   495 │   │   if ptr == ffi.NULL:                                           │
│ > 496 │   │   │   raise WebPError('failed to create decoder')               │
│   497 │   │   anim_info = WebPAnimInfo.new()                                │
│   498 │   │   if lib.WebPAnimDecoderGetInfo(ptr, anim_info.ptr) == 0:       │
│   499 │   │   │   raise WebPError('failed to get animation info')

I tried to dig deeper to see if I could figure out why ptr is null, but at that point it looked like I was getting into the compiled C++ code and couldn't really explore further.

I've reproduced the error with a couple of different animated webp files now, just to make sure the issue wasn't with the file itself. Interestingly, I tried also using Pillow's WebPImagePlugin, only to find that it gave me the same error, and was just using this library under the hood 😅

I think this is likely an issue with my installation of pywebp/local setup, rather than an issue with the library, but any advice would be appreciated!

anibali commented 11 months ago

Interestingly, I tried also using Pillow's WebPImagePlugin, only to find that it gave me the same error, and was just using this library under the hood

To my knowledge, Pillow actually has its own bindings for webp (it does not use my bindings). Therefore if you found the same issue from both then it points to an issue with your environment or perhaps the files themselves. Unfortunately I can't really help much further.