Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RuntimeError in ImageFile.Parser.feed for WebP #4745

Closed
vetal4444 opened this issue Jun 29, 2020 · 2 comments · Fixed by #5404
Closed

RuntimeError in ImageFile.Parser.feed for WebP #4745

vetal4444 opened this issue Jun 29, 2020 · 2 comments · Fixed by #5404
Labels

Comments

@vetal4444
Copy link

vetal4444 commented Jun 29, 2020

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)
@hugovk hugovk added the WebP label Jun 29, 2020
@radarhere
Copy link
Member

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)

@radarhere
Copy link
Member

I've created PR #5404 to fix this. If you could test it and confirm that this fixes your issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants