[Image-SIG] Image.crop with bbox off image

Fredrik Lundh fredrik at pythonware.com
Fri Jul 4 19:14:00 CEST 2008

Gautham Narayan wrote:

> I'm having trouble understanding the behaviour of Image.crop (PIL
> 1.1.6 w/ python 2.5.1) when the bbox is outside the limits of the
> image. So something like,
> b = a.crop(x1,y1,x2,y2)
> with x1 and y1 < 0 say.
> Sometimes I get a purely black border (which is good), sometimes I get
> the image edges and patterns that are not in the image beyond. Is
> there some way I can specify a crop that is off the image in at least
> one coordinate and consistently have the region that is off the image
> padded with 0s? Otherwise is there another alternative to checking if
> the bbox is off the image and setting the remaining values to zero?

I think the clamping in the 'crop' method messes up in some cases; to 
get more reliable behaviour, you might want to try the transform(EXTENT)
method instead.


More information about the Image-SIG mailing list