diff --git a/pelican/plugins/image_process/image_process.py b/pelican/plugins/image_process/image_process.py index 2cb58ec..898a708 100644 --- a/pelican/plugins/image_process/image_process.py +++ b/pelican/plugins/image_process/image_process.py @@ -665,6 +665,27 @@ def process_picture(soup, img, group, settings, derivative): img.insert_before(s["element"]) +def try_open_image(path): + try: + i = Image.open(path) + except UnidentifiedImageError: + logger.warning( + "%s Source image %s is not supported by Pillow.", + LOG_PREFIX, + path, + ) + return None + except FileNotFoundError: + logger.warning( + "%s Source image %s not found.", + LOG_PREFIX, + path, + ) + return None + + return i + + def process_image(image, settings): # Set default value for 'IMAGE_PROCESS_FORCE'. if "IMAGE_PROCESS_FORCE" not in settings: @@ -686,21 +707,8 @@ def process_image(image, settings): ): logger.debug("{} Processing {} -> {}".format(LOG_PREFIX, image[0], image[1])) - try: - i = Image.open(image[0]) - except UnidentifiedImageError: - logger.warning( - "%s Source image %s is not supported by Pillow.", - LOG_PREFIX, - image[0], - ) - return - except FileNotFoundError: - logger.warning( - "%s Source image %s not found.", - LOG_PREFIX, - image[0], - ) + i = try_open_image(image[0]) + if i is None: return for step in image[2]: diff --git a/pelican/plugins/image_process/test_image_process.py b/pelican/plugins/image_process/test_image_process.py index c31ed10..448791b 100644 --- a/pelican/plugins/image_process/test_image_process.py +++ b/pelican/plugins/image_process/test_image_process.py @@ -12,7 +12,7 @@ ExifTool, compute_paths, harvest_images_in_fragment, - is_img_identifiable, + try_open_image, process_image, ) @@ -118,11 +118,6 @@ def test_all_transforms(tmp_path, transform_id, transform_params, image_path): ], ) def test_path_normalization(mocker, orig_src, orig_img, new_src, new_img): - # Allow non-existing images to be processed: - mocker.patch( - "pelican.plugins.image_process.image_process.is_img_identifiable", - lambda img_filepath: True, - ) # Silence image transforms. process = mocker.patch("pelican.plugins.image_process.image_process.process_image") @@ -439,11 +434,6 @@ def test_path_normalization(mocker, orig_src, orig_img, new_src, new_img): ], ) def test_picture_generation(mocker, orig_tag, new_tag, call_args): - # Allow non-existing images to be processed: - mocker.patch( - "pelican.plugins.image_process.image_process.is_img_identifiable", - lambda img_filepath: True, - ) process = mocker.patch("pelican.plugins.image_process.image_process.process_image") settings = get_settings( @@ -558,19 +548,19 @@ def test_copy_exif_tags(tmp_path, image_path, copy_tags): assert tag not in actual_tags.keys() -def test_is_img_identifiable(): +def test_try_open_image(): for test_image in TEST_IMAGES: - assert is_img_identifiable(test_image) + assert try_open_image(test_image) - assert not is_img_identifiable("image/that/does/not/exist.png") + assert not try_open_image("image/that/does/not/exist.png") - assert not is_img_identifiable(TEST_DATA.joinpath("folded_puzzle.png")) - assert not is_img_identifiable(TEST_DATA.joinpath("minimal.svg")) + assert not try_open_image(TEST_DATA.joinpath("folded_puzzle.png")) + assert not try_open_image(TEST_DATA.joinpath("minimal.svg")) img = {"src": "https://upload.wikimedia.org/wikipedia/commons/3/34/Exemple.png"} settings = get_settings(IMAGE_PROCESS_DIR="derivatives") path = compute_paths(img, settings, derivative="thumb") - assert not is_img_identifiable(path.source) + assert not try_open_image(path.source) def generate_test_images():