Trouble using python PIL library to crop and save image

user124626 picture user124626 · Jul 2, 2009 · Viewed 48.7k times · Source

Im attempting to crop a pretty high res image and save the result to make sure its completed. However I keep getting the following error regardless of how I use the save method: SystemError: tile cannot extend outside image

from PIL import Image

# size is width/height
img = Image.open('0_388_image1.jpeg')
box = (2407, 804, 71, 796)
area = img.crop(box)

area.save('cropped_0_388_image1', 'jpeg')
output.close()

Answer

RichieHindle picture RichieHindle · Jul 2, 2009

The box is (left, upper, right, lower) so maybe you meant (2407, 804, 2407+71, 804+796)?

Edit: All four coordinates are measured from the top/left corner, and describe the distance from that corner to the left edge, top edge, right edge and bottom edge.

Your code should look like this, to get a 300x200 area from position 2407,804:

left = 2407
top = 804
width = 300
height = 200
box = (left, top, left+width, top+height)
area = img.crop(box)