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

Gautham Narayan gsnarayan at gmail.com
Tue Jun 17 19:48:42 CEST 2008

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?

>>> import Image
>>> a=Image.open("work/screen/defaults/illumination5.bmp")
>>> a.crop((-100,-100,10,10)).show()

#The above has a pure black border

>>> a.crop((-500,-200,10,10)).show()
>>> a.crop((-500,-400,50,10)).show()

#These above two do not

>>> print a.__dict__
{'info': {'compression': 0}, 'category': 0, 'palette': None,
'decoderconfig': (), 'map': <mmap.mmap object at 0xb7b58de0>,
'filename': 'work/screen/defaults/illumination5.bmp', 'fp': None,
'readonly': 1, 'im': <ImagingCore object at 0xb7d110c0>, 'mode': 'L',
'decodermaxblock': 65536, 'tile': [], 'size': (1000, 1000)}

The file itself is a simple 1000x1000 image off a gaussian fall off
but the same behaviour happens with any image I try, irrespective of
dimensions and content. Thanks!


More information about the Image-SIG mailing list