Skip to content

Conversation

@minchinweb
Copy link
Member

@minchinweb minchinweb commented Jan 13, 2026

Current, if there's an image that Pillow can't process (e.g. an svg files), the plugin prints an error message but still changes the img src location. This means that the image in the final site won't load.

This change makes it to the img src is only changed if the image is processed without raising an error.


Ruff complains about the immediately re-raised error at about Line 760, but I've kept it to make it more obvious that the errors are expected to be raised at that location, and to make it clear that the function might be exited at that location. I've applied #noqa: TRY203 to that line to silence this particular warning.

Ruff prints the following:

TRY203 Remove exception handler; error is immediately re-raised
   --> pelican\plugins\image_process\image_process.py:760:9
    |
758 |           try:
759 |               i = try_open_image(image[0])
760 | /         except (UnidentifiedImageError, FileNotFoundError):
761 | |             # return None
762 | |             raise
    | |_________________^
763 |
764 |           for step in image[2]:
    |

Edit: I've changed the log level for skipped images to INFO (but left unfound images at WARNING). Partly, this is because I publish my site with fatal warnings, and so don't want it to break here.

minchinweb added a commit to minchinweb/blog.minchin.ca that referenced this pull request Jan 13, 2026
Copy link
Collaborator

@patrickfournier patrickfournier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR. This is a helpful improvement that makes the plugin easier to use.

However your PR is incomplete. You have modified the most common usage (process_img_tag()). You should also make sure other cases adopt the same behavior and handle the exceptions raised by process_picture(). The functions build_srcset(), convert_div_to_picture_tag(), process_picture() and process_metadata() would all need to be modified to make the new behavior happen in all cases.

We also should have tests for this new behavior.

logger.warning(
f'{LOG_PREFIX} Source image "{path}" is not supported by Pillow.'
)
logger.info(f'{LOG_PREFIX} Source image "{path}" is not supported by Pillow.')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
logger.info(f'{LOG_PREFIX} Source image "{path}" is not supported by Pillow.')
logger.info(f'{LOG_PREFIX} Source image "{path}" will not be processed because it is not supported by Pillow.')

except (UnidentifiedImageError, FileNotFoundError):
return None
except (UnidentifiedImageError, FileNotFoundError): # noqa: TRY203
# return None
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# return None

img["srcset"] = ", ".join(srcset)


def convert_div_to_picture_tag(soup, img, group, settings, derivative):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function should be modified so that original images are used if process_image() fails. I think the correct behavior in this case would be to use the original images in the <picture>.

return f"{path} {condition}"


def build_srcset(img, settings, derivative):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function should be modified to behave gracefully when process_image raises an exception. I think the correct behavior would be to use the original image for src and to not add the srcset attribute.

img.wrap(picture_tag)


def process_picture(soup, img, group, settings, derivative):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function should be modified so that original images are used if process_image() fails. I think the correct behavior in this case would be to use the original images in the <picture>.

path = compute_paths(value, generator.context, derivative)

original_values[key] = value
metadata[key] = urljoin(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image paths in metadata should not be changed if process_images raises an exception.

@minchinweb
Copy link
Member Author

Ok, I'll work on this, but it will take a while to work through, as I don't think I've used anything other that <img> processing before. I expect I'll probably have to set up a test site as part of the that process...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants