[PYTHON IMAGE-SIG] The crop method doesn't work properly for palette images

Fredrik Lundh fredrik_lundh@ivab.se
Fri, 17 Jan 1997 21:27:00 +0100


A last-minute workaround to stop "crop" from crashing if the box
extended outside the source image, messed things up when working with
palette images.  To fix this, replace the "crop" method in Image.py
with the following code:

    def crop(self, box = None):
        "Crop region from image"

	self.load()
	if box == None:
	    return self.copy()

	# the C implementation of crop is broken, so we implement
	# it by pasting into empty image instead.

	# im = self.im.crop(box)

	size = box[2]-box[0], box[3]-box[1]

	im = core.new(self.mode, size)
	im.paste(self.im, (-box[0], -box[1],
		 self.size[0]-box[0],
		 self.size[1]-box[1]))

	imOut = self._makeself(im)

	if imOut.mode == "P":
	    imOut.im.putpalette("RGB", self.im.getpalette("RGB"))

	return imOut

In future versions, this will be fixed in the C implementation,
reducing the above code to a simple call to self.im.crop() instead.

Regards	/F

_______________
IMAGE-SIG - SIG on Image Processing with Python

send messages to: image-sig@python.org
administrivia to: image-sig-request@python.org
_______________