Pillow: RuntimeError in ImageFile.Parser.feed for WebP

Created on 29 Jun 2020  路  1Comment  路  Source: python-pillow/Pillow

What did you do?

Use ImageFile.Parser.feed to get some image metadata without full image download.

What did you expect to happen?

Get ImageFile.Parser.image instance

What actually happened?

Traceback (most recent call last):
  File "./reproduce.py", line 7, in <module>
    p.feed(new_data)
  File "/home/test/env/lib/python3.8/site-packages/PIL/ImageFile.py", line 411, in feed
    im = Image.open(fp)
  File "/home/test/env/lib/python3.8/site-packages/PIL/Image.py", line 2885, in open
    im = _open_core(fp, filename, prefix)
  File "/home/test/env/lib/python3.8/site-packages/PIL/Image.py", line 2867, in _open_core
    im = factory(fp, filename)
  File "/home/test/env/lib/python3.8/site-packages/PIL/ImageFile.py", line 107, in __init__
    self._open()
  File "/home/test/env/lib/python3.8/site-packages/PIL/WebPImagePlugin.py", line 60, in _open
    self._decoder = _webp.WebPAnimDecoder(self.fp.read())
RuntimeError: could not create decoder object

What are your OS, Python and Pillow versions?

  • OS: ArchLinux. Same for Ubuntu 20.04.
  • Python: 3.8.2
  • Pillow: 7.1.2

This code worked for JPEG but failed for WebP:

from PIL import ImageFile

p = ImageFile.Parser()
with open("./img.webp", "rb") as f:
    new_data = f.read(1024)
    while not p.image and new_data:
        p.feed(new_data)
        new_data = f.read(1024)
    print(p.image.size)

WebP

Most helpful comment

As an immediate workaround, using one of the test images I find that just catching the error and continuing through the loop works.

from PIL import ImageFile

p = ImageFile.Parser()
with open("Tests/images/hopper.webp", "rb") as f:
    new_data = f.read(1024)
    while not p.image and new_data:
        try:
            p.feed(new_data)
        except RuntimeError:
            pass
        new_data = f.read(1024)
    print(p.image.size)

>All comments

As an immediate workaround, using one of the test images I find that just catching the error and continuing through the loop works.

from PIL import ImageFile

p = ImageFile.Parser()
with open("Tests/images/hopper.webp", "rb") as f:
    new_data = f.read(1024)
    while not p.image and new_data:
        try:
            p.feed(new_data)
        except RuntimeError:
            pass
        new_data = f.read(1024)
    print(p.image.size)
Was this page helpful?
0 / 5 - 0 ratings

Related issues

indirectlylit picture indirectlylit  路  4Comments

amithnikhade picture amithnikhade  路  4Comments

mmalenta picture mmalenta  路  3Comments

thinrhino picture thinrhino  路  3Comments

nomarek picture nomarek  路  3Comments