使用python PIL库来裁剪和保存图像时遇到困难

我试图裁剪一个相当高分辨率的图像,并保存结果,以确保其完成。 但是,不pipe我如何使用保存方法,我总是收到以下错误: 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() 

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

编辑 :所有四个坐标都是从顶部/左侧angular度测量的,并描述从该angular到左侧边缘,顶部边缘,右侧边缘和底部边缘的距离。

你的代码应该是这样的,从位置2407,804获得一个300×200的区域:

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

尝试这个:

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

 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() 

在这种情况下,input图像是1280 x 800像素和croped是1280 x 399px从左上angular开始。