-
-
Notifications
You must be signed in to change notification settings - Fork 22
Skip images that Pillow can't process #100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
relies on Image Process pre-release; see pelican-plugins/image-process#100
There was a problem hiding this 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.') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| # return None |
| img["srcset"] = ", ".join(srcset) | ||
|
|
||
|
|
||
| def convert_div_to_picture_tag(soup, img, group, settings, derivative): |
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
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.
|
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 |
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: TRY203to that line to silence this particular warning.Ruff prints the following:
Edit: I've changed the log level for skipped images to
INFO(but left unfound images atWARNING). Partly, this is because I publish my site with fatal warnings, and so don't want it to break here.