2016-10-01 49 views
1
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 resulution不小480x480PIL裁剪图像给予高度不正确的结果

但是当我此行更改为

tall = height-rm_height/2 # tall = 600 

输出图像是方形小480x480

它不会使感。我做错了什么。谢谢

回答

-1

确定后,我搜索更多。现在我把它从这个post

克里斯·克拉克(由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)) 

它并不高。它是底部

+0

这是什么说的? – physicalattraction

+0

来自克里斯克拉克,这是说im.crop((左,上,右,下)) – gongsun