wagtail / Willow

A wrapper that combines the functionality of multiple Python image libraries into one API
https://willow.wagtail.org/
BSD 3-Clause "New" or "Revised" License
273 stars 53 forks source link

Gracefully handle out-of-bounds crops #92

Closed gasman closed 2 years ago

gasman commented 2 years ago

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 https://github.com/wagtail/wagtail/issues/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 https://github.com/wagtail/wagtail/issues/7766, although we'll also want to fix the rounding error that leads to the out-of-bounds condition in the first place.