python-imaging-library - 使用 python PIL库裁剪和保存图像的问题

不管如何使用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()

时间:

你的代码应如下所示:


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

在这种情况下,输入图像为1280 x800像素,裁剪的图像为1280 x399像素(从左上角开始)。

...