Skip to content

Commit

Permalink
Allow raising exceptions. LOAD_TRUNCATED_IMAGES (prevents crash on is…
Browse files Browse the repository at this point in the history
…sue #14, but more testing is necessary to see if image actually is loaded).
  • Loading branch information
Poikilos committed Apr 9, 2024
1 parent c47e7a7 commit c70869e
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions channeltinkerpil/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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.
Expand Down Expand Up @@ -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),
Expand All @@ -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.
Expand Down

0 comments on commit c70869e

Please sign in to comment.