PIL作物图像给出了不正确的高度结果

2024-03-28 12:58:13 发布

您现在位置:Python中文网/ 问答频道 /正文

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

但当我把这条线改成

^{pr2}$

输出图像为正方形480x480

这没道理。我做错了什么。谢谢


Tags: rmfrom图像cropimageimportimgpil
1条回答
网友
1楼 · 发布于 2024-03-28 12:58:13

好吧,等我搜索更多。现在我从这个post得到它

克里斯·克拉克(San4ez)的回答是:

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

它不高。它是底部

相关问题 更多 >