43 python python-imaging-library
我试图裁剪一个相当高的res图像并保存结果,以确保它完成.但是,无论我如何使用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()
Run Code Online (Sandbox Code Playgroud)
Ric*_*dle 66
盒子是(左,上,右,下)所以你的意思是(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)
Run Code Online (Sandbox Code Playgroud)
小智 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()
Run Code Online (Sandbox Code Playgroud)
在这种情况下,输入图像为1280 x 800像素,从左上角开始弯曲为1280 x 399像素.