47

我试图裁剪一个非常高分辨率的图像并保存结果以确保其完成。但是,无论我如何使用 save 方法,我都会收到以下错误: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()
4

2 回答 2

69

盒子是(左,上,右,下)所以也许你的意思是(2407、804、2407+71、804+796)?

编辑:所有四个坐标都是从上/左角开始测量的,并描述了从那个角到左边缘、上边缘、右边缘和下边缘的距离。

您的代码应如下所示,从位置 2407,804 获取 300x200 区域:

left = 2407
top = 804
width = 300
height = 200
box = (left, top, left+width, top+height)
area = img.crop(box)
于 2009-07-02T20:56:50.663 回答
15

试试这个:

这是一个裁剪图像的简单代码,它就像一个魅力;)

import Image

def crop_image(input_image, output_image, start_x, start_y, width, height):
    """Pass input name image, output name image, x coordinate to start croping, y coordinate to start croping, width to crop, height to crop """
    input_img = Image.open(input_image)
    box = (start_x, start_y, start_x + width, start_y + height)
    output_img = input_img.crop(box)
    output_img.save(output_image +".png")

def main():
    crop_image("Input.png","output", 0, 0, 1280, 399)

if __name__ == '__main__': main()

在这种情况下,输入图像为 1280 x 800 像素,从左上角开始裁剪为 1280 x 399 像素。

于 2013-07-15T11:28:06.453 回答