Skip to content
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

Gracefully handle out-of-bounds crops #92

Merged
merged 1 commit into from Feb 25, 2022

Conversation

gasman
Copy link
Contributor

@gasman gasman commented Feb 21, 2022

When passed a crop rectangle that extends outside an image's boundaries, Pillow gracefully returns an image of the requested size (padding with background / transparent pixels as required), but Wand throws a value error. If the calling app is sloppy about dimensions (as is apparently the case in wagtail/wagtail#7766), this will result in errors that only surface when Wand is in use (i.e. for animated gifs).

This PR gives the two backends a consistent "middle ground" behaviour that hopefully makes intuitive sense: the crop rectangle is clamped to the image boundary so that if it overlaps, the final image is smaller than requested. Totally out of bounds or nonsensical parameters (e.g. left > right) will raise a BadImageOperationError.

Fixes wagtail/wagtail#7766, although we'll also want to fix the rounding error that leads to the out-of-bounds condition in the first place.

When passed a crop rectangle that extends outside an image's boundaries, Pillow gracefully returns an image of the requested size (padding with background / transparent pixels as required), but Wand throws a value error. If the calling app is sloppy about dimensions (as is apparently the case in wagtail/wagtail#7766), this will result in errors that only surface when Wand is in use (i.e. for animated gifs).

This PR gives the two backends a consistent "middle ground" behaviour that hopefully makes intuitive sense: the crop rectangle is clamped to the image boundary so that if it overlaps, the final image is smaller than requested. Totally out of bounds or nonsensical parameters (e.g. left > right) will raise a BadImageOperationError.

Fixes wagtail/wagtail#7766, although we'll also want to fix the rounding error that leads to the out-of-bounds condition in the first place.
Copy link
Contributor

@kaedroho kaedroho left a comment

Choose a reason for hiding this comment

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

This looks good to me, thanks!

@gasman gasman merged commit 0b1168a into wagtail:main Feb 25, 2022
gasman added a commit that referenced this pull request Feb 25, 2022
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.

max filter breaks animated gifs
2 participants