From c70869e81a75dd337cb0ae9d29aa1cea39802d65 Mon Sep 17 00:00:00 2001 From: poikilos <7557867+poikilos@users.noreply.github.com> Date: Mon, 8 Apr 2024 21:44:51 -0400 Subject: [PATCH] Allow raising exceptions. LOAD_TRUNCATED_IMAGES (prevents crash on issue #14, but more testing is necessary to see if image actually is loaded). --- channeltinkerpil/__init__.py | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/channeltinkerpil/__init__.py b/channeltinkerpil/__init__.py index c26d746..08c03d8 100644 --- a/channeltinkerpil/__init__.py +++ b/channeltinkerpil/__init__.py @@ -2,8 +2,9 @@ import os from channeltinker import diff_images -from PIL import Image import PIL +from PIL import Image, ImageFile + from channeltinker import ( # echo0, echo1, @@ -12,6 +13,8 @@ echo4, ) +ImageFile.LOAD_TRUNCATED_IMAGES = True + def gen_diff_image(base, head, diff=None, diff_path=None): """Compare two PIL-compatible image objects visually. @@ -81,15 +84,27 @@ def gen_diff_image(base, head, diff=None, diff_path=None): return result -def diff_images_by_path(base_path, head_path, diff_path=None): +def diff_images_by_path(base_path, head_path, diff_path=None, + raise_exceptions=False): """Compare two images. See gen_diff_image for further info. This function only checks sanity then calls gen_diff_image. + + Args: + base_path (str): Any image. + head_path (str): Any image to compare to base_path. + diff_path (str, optional): Where to save a visualization image + to highlight differences. + raise_exception (bool, optional): Raise exception instead + of setting {'base': {"error": error}} + or {'head': {"error": error}} """ result = None try: base = Image.open(base_path) except PIL.UnidentifiedImageError as ex: + if raise_exceptions: + raise result = { 'base': { 'error_type': type(ex), @@ -101,6 +116,8 @@ def diff_images_by_path(base_path, head_path, diff_path=None): try: head = Image.open(head_path) except PIL.UnidentifiedImageError as ex: + if raise_exceptions: + raise result2 = { 'base': { }, # It must be a dict to prevent a key error.