I\'m trying to crop an image and then paste the cropped image into the centre of another image. Ideally I\'d like the cropped image to be smaller than the image its being pa
The PIL documentation for the crop method states:
Returns a rectangular region from the current image. The box is a 4-tuple defining the left, upper, right, and lower pixel coordinate.
This is a lazy operation. Changes to the source image may or may not be reflected in the cropped image. To get a separate copy, call the load method on the cropped copy.
So, you should try to make sure you get an actual cropped copy.region = House.crop(box).load()
UPDATE:
Actually, it seems the above only works if you're using PIL 1.1.6 and later. In versions before that, I guess load()
doesn't return anything so you can't chain the operations. In that case, use:
region = House.crop(box)
region.load()
I had a similar error that I could not seem to solve, but I then realized as you did that it had to do with the arguments passed in to Image.crop(). You can see the size of your image is (0,0) so there is nothing to show. You are setting bounds from point (25,25) to (25,25).
If you need a 25x25 cropped image(starting from the top left): ``` >
>> import Image
>>> grey = Image.new('RGB', (200, 200), "grey")
>>> House = Image.open("House01.jpg")
>>> print grey.size, grey.mode, grey.format
>>>(200, 200) RGB None
>>> print House.size, House.mode, House.format
>>>(300, 300) RGB JPEG
>>> box = (0, 0, 25, 25)
>>> House.crop(box)
>>>Image._ImageCrop image mode=RGB size=0x0 at 0x11AD210>
>>> region = House.crop(box)
>>> region.show()
``` If you want to start from the center or another point I would use this link as a reference: