PIL裁剪图像给出不正确的高度结果

时间:2016-10-01 10:55:58

标签: python python-2.7 python-imaging-library

import PIL
from PIL import Image

img = Image.open('0009_jpg.jpg')

width, height = img.size #height is 720 and width is 480

if height > width:
    rm_height = abs(height - width) # rm_height = 240
    x_offset = 0
    y_offset = rm_height/2 # y_offset = 120
    tall = height-rm_height # tall = 480
    img_crop = img.crop((x_offset, y_offset, width, tall))

img_crop.save('crop_jpg.jpg')

输出图像为480x360,而不是480x480

但当我将此行更改为

tall = height-rm_height/2 # tall = 600

输出图像为方形480x480

这没有意义。我做错了什么。感谢

1 个答案:

答案 0 :(得分:-1)

在我搜索更多内容后确定。现在我从这个post

得到它

Chris Clarke(San4ez的edited)回答:

import Image
im = Image.open(<your image>)
width, height = im.size   # Get dimensions

left = (width - new_width)/2
top = (height - new_height)/2
right = (width + new_width)/2
bottom = (height + new_height)/2

im.crop((left, top, right, bottom))

它不高。它是底部