有没有我忽略的明显的方法?我只是想做个缩略图。


当前回答

######get resize coordinate after resize the image using this function#####
def scale_img_pixel(points,original_dim,resize_dim):
        multi_list = [points]
        new_point_list = []
        multi_list_point = []
        for point in multi_list:
            multi_list_point.append([point[0],point[1]])
            multi_list_point.append([point[2],point[3]])
        for lsingle_point in multi_list_point:
            x1 = int((lsingle_point[0] * (resize_dim[0] / original_dim[0])))
            y1 = int((lsingle_point[1] * (resize_dim[1] / original_dim[1])))
            new_point_list.append(x1)
            new_point_list.append(y1)
            
        return new_point_list
    
    
    points = [774,265,909,409]
    original_dim = (1237,1036)
    resize_dim = (640,480)
    result = scale_img_pixel(points,original_dim,resize_dim)
    print("result: ", result)  

其他回答

######get resize coordinate after resize the image using this function#####
def scale_img_pixel(points,original_dim,resize_dim):
        multi_list = [points]
        new_point_list = []
        multi_list_point = []
        for point in multi_list:
            multi_list_point.append([point[0],point[1]])
            multi_list_point.append([point[2],point[3]])
        for lsingle_point in multi_list_point:
            x1 = int((lsingle_point[0] * (resize_dim[0] / original_dim[0])))
            y1 = int((lsingle_point[1] * (resize_dim[1] / original_dim[1])))
            new_point_list.append(x1)
            new_point_list.append(y1)
            
        return new_point_list
    
    
    points = [774,265,909,409]
    original_dim = (1237,1036)
    resize_dim = (640,480)
    result = scale_img_pixel(points,original_dim,resize_dim)
    print("result: ", result)  

基于@tomvon,我完成了以下使用(选择你的案例):

a)调整高度(我知道新的宽度,所以我需要新的高度)

new_width  = 680
new_height = new_width * height / width 

b)调整宽度(我知道新的高度,所以我需要新的宽度)

new_height = 680
new_width  = new_height * width / height

然后:

img = img.resize((new_width, new_height), Image.ANTIALIAS)

这个脚本将使用PIL (Python成像库)将图像(somepic.jpg)调整为300像素的宽度和与新宽度成比例的高度。它通过确定300像素是原始宽度(img.size[0])的百分比,然后将原始高度(img.size[1])乘以该百分比来实现这一点。将“basewidth”更改为任何其他数字以更改图像的默认宽度。

from PIL import Image

basewidth = 300
img = Image.open('somepic.jpg')
wpercent = (basewidth/float(img.size[0]))
hsize = int((float(img.size[1])*float(wpercent)))
img = img.resize((basewidth,hsize), Image.Resampling.LANCZOS)
img.save('somepic.jpg')

如果你试图保持相同的纵横比,那么你不会调整原始大小的一些百分比吗?

例如,原来大小的一半

half = 0.5
out = im.resize( [int(half * s) for s in im.size] )
from PIL import Image

img = Image.open('/your image path/image.jpg') # image extension *.png,*.jpg
new_width  = 200
new_height = 300
img = img.resize((new_width, new_height), Image.ANTIALIAS)
img.save('output image name.png') # format may what you want *.png, *jpg, *.gif