From dd030343b8adf61e0b8ace836952d815c70ff46b Mon Sep 17 00:00:00 2001 From: Mattijs Ugen <144798+akaIDIOT@users.noreply.github.com> Date: Fri, 18 Feb 2022 14:11:05 +0100 Subject: [PATCH] Use importlib.metadata.entry_points to load PIL.Image.decoder entries on init() --- src/PIL/Image.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/PIL/Image.py b/src/PIL/Image.py index 8d36e8871..4dccaebab 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -44,6 +44,12 @@ try: except ImportError: ElementTree = None +try: + from importlib.metadata import entry_points +except ImportError: + # NB: potentially try to load entry_points from backported importlib_metadata? + entry_points = None + # VERSION was removed in Pillow 6.0.0. # PILLOW_VERSION was removed in Pillow 9.0.0. # Use __version__ instead. @@ -374,6 +380,12 @@ def init(): except ImportError as e: logger.debug("Image: failed to import %s: %s", plugin, e) + if entry_points: + for decoder in entry_points(group='PIL.Image.decoder'): + # or, alternatively, register a decoder directly from here: + # DECODERS[decoder.name] = decoder.load() + decoder.load() + if OPEN or SAVE: _initialized = 2 return 1