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

Unpopular formats argument of PIL.Image.open cause exceptions #5036

Closed
tsangwpx opened this issue Nov 10, 2020 · 2 comments · Fixed by #5037
Closed

Unpopular formats argument of PIL.Image.open cause exceptions #5036

tsangwpx opened this issue Nov 10, 2020 · 2 comments · Fixed by #5037

Comments

@tsangwpx
Copy link

tsangwpx commented Nov 10, 2020

In open(), preinit() only initialize popular formats,
_open_core() will raise if the given formats have not been initialized. The init() failback become unreachable.

Pillow/src/PIL/Image.py

Lines 2901 to 2934 in 2d6e51e

preinit()
accept_warnings = []
def _open_core(fp, filename, prefix, formats):
for i in formats:
try:
factory, accept = OPEN[i]
result = not accept or accept(prefix)
if type(result) in [str, bytes]:
accept_warnings.append(result)
elif result:
fp.seek(0)
im = factory(fp, filename)
_decompression_bomb_check(im.size)
return im
except (SyntaxError, IndexError, TypeError, struct.error):
# Leave disabled by default, spams the logs with image
# opening failures that are entirely expected.
# logger.debug("", exc_info=True)
continue
except BaseException:
if exclusive_fp:
fp.close()
raise
return None
im = _open_core(fp, filename, prefix, formats)
if im is None:
if init():
im = _open_core(fp, filename, prefix, formats)

What did you do?

from __future__ import annotations

from PIL import Image


def main():
    with open('any.webp', 'rb') as f:
        try:
            im = Image.open(f, formats=('WEBP',))
        except Exception:
            print(Image.ID)  # No WEBP in the list
            raise
        else:
            print(im.size)


if __name__ == '__main__':
    main()

What did you expect to happen?

Output the size

(640, 480)

What actually happened?

['BMP', 'DIB', 'GIF', 'TIFF', 'JPEG', 'PPM', 'PNG']
Traceback (most recent call last):
  File "pil_debug.py", line 18, in <module>
    main()
  File "pil_debug.py", line 9, in main
    im = Image.open(f, formats=('WEBP',))
  File "lib\site-packages\PIL\Image.py", line 2929, in open
    im = _open_core(fp, filename, prefix, formats)
  File "lib\site-packages\PIL\Image.py", line 2909, in _open_core
    factory, accept = OPEN[i]
KeyError: 'WEBP'

What are your OS, Python and Pillow versions?

  • OS: Windows 10
  • Python: 3.8
  • Pillow: 8.0.1
@nulano
Copy link
Contributor

nulano commented Nov 11, 2020

You can import the WebP plugin explicitly to make sure it is loaded:

from PIL import Image, WebPImagePlugin
im = Image.open("Downloads/1.webp", formats=("WEBP",))
# im = <PIL.WebPImagePlugin.WebPImageFile image mode=RGB size=550x368 at 0x233A9546508>

@radarhere radarhere changed the title Unpopular formats argument of PIL.Image.open cause exceptions. Unpopular formats argument of PIL.Image.open cause exceptions Nov 11, 2020
@radarhere
Copy link
Member

I've created PR #5037 to resolve this.

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

Successfully merging a pull request may close this issue.

3 participants