Use ImageFile.Parser.feed to get some image metadata without full image download.
Get ImageFile.Parser.image instance
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
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)
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)
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.