2016-10-01 77 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