diff --git a/dlibrary/dlibrary.py b/dlibrary/dlibrary.py index 38b0cfb..0f73a8e 100755 --- a/dlibrary/dlibrary.py +++ b/dlibrary/dlibrary.py @@ -22,7 +22,8 @@ import zipfile import dlsite_async import fitz from libsixel import * -from PIL import Image +from PIL import Image, UnidentifiedImageError +import PIL.ImageFile from jinja2 import Environment, PackageLoader, select_autoescape import pyuca import rarfile @@ -918,8 +919,17 @@ def descendant_files_ignore(path, exclude): return result def standalone_image_size(filepath): - with Image.open(filepath) as im: - return im.size + try: + with Image.open(filepath) as im: + return im.size + except UnidentifiedImageError: + print(f'Warning: PIL failed to load image {filepath}! Retrying with less strict settings') + PIL.ImageFile.LOAD_TRUNCATED_IMAGES = True + try: + with Image.open(filepath) as im: + return im.size + finally: + PIL.ImageFile.LOAD_TRUNCATED_IMAGES = False def pdf_image_sizes(filepath): sizes_by_xref = {}