From 987f427c56a9f17be251d52e71fad73c57b6adca Mon Sep 17 00:00:00 2001 From: xenofem Date: Sun, 14 Apr 2024 18:56:19 -0400 Subject: [PATCH] accept PNGs that have broken CRC32 checksums for ancillary chunks, which is apparently a thing we need to deal with??? --- dlibrary/dlibrary.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) 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 = {}