From 132464c0a1ce199e3af2be8dca39bb4aa7691686 Mon Sep 17 00:00:00 2001 From: mara004 Date: Sun, 25 Aug 2024 17:43:53 +0200 Subject: [PATCH 1/2] Slightly simplify imgformat retrieval No need for a loop here - we can access the enum like a dictionary, which should be more efficient. --- src/img2pdf.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/img2pdf.py b/src/img2pdf.py index f89670b..76439c3 100755 --- a/src/img2pdf.py +++ b/src/img2pdf.py @@ -1832,11 +1832,9 @@ def read_images( ) else: logger.debug("PIL format = %s", imgdata.format) - imgformat = None - for f in ImageFormat: - if f.name == imgdata.format: - imgformat = f - if imgformat is None: + try: + imgformat = ImageFormat[imgdata.format] + except KeyError: imgformat = ImageFormat.other def cleanup(): -- 2.39.5 From 2b563c0982d59d9d75c252d91930ed1ea73a611d Mon Sep 17 00:00:00 2001 From: mara004 Date: Tue, 27 Aug 2024 22:26:22 +0200 Subject: [PATCH 2/2] Update to `getattr()` This now a one-liner, compared to 6 lines originally. And it should be (algorithmically) faster. --- src/img2pdf.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/img2pdf.py b/src/img2pdf.py index 76439c3..3b9ba99 100755 --- a/src/img2pdf.py +++ b/src/img2pdf.py @@ -1832,10 +1832,7 @@ def read_images( ) else: logger.debug("PIL format = %s", imgdata.format) - try: - imgformat = ImageFormat[imgdata.format] - except KeyError: - imgformat = ImageFormat.other + imgformat = getattr(ImageFormat, imgdata.format, ImageFormat.other) def cleanup(): if imgdata is not None: -- 2.39.5