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


当前回答

打开图像文件

from PIL import Image
im = Image.open("image.png")

使用PIL Image。Resize (size, resample=0)方法,其中将图像的(宽度,高度)替换为2元组大小。

这将显示原始大小的图像:

display(im.resize((int(im.size[0]),int(im.size[1])), 0) )

这将显示图像大小的1/2:

display(im.resize((int(im.size[0]/2),int(im.size[1]/2)), 0) )

这将显示你的图像的1/3大小:

display(im.resize((int(im.size[0]/3),int(im.size[1]/3)), 0) )

这将显示你的图像的1/4大小:

display(im.resize((int(im.size[0]/4),int(im.size[1]/4)), 0) )

等等

其他回答

######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)  
from PIL import Image
from resizeimage import resizeimage

def resize_file(in_file, out_file, size):
    with open(in_file) as fd:
        image = resizeimage.resize_thumbnail(Image.open(fd), size)
    image.save(out_file)
    image.close()

resize_file('foo.tif', 'foo_small.jpg', (256, 256))

我使用这个库:

pip install python-resize-image

如果你不想/不需要用Pillow打开图像,使用这个:

from PIL import Image

new_img_arr = numpy.array(Image.fromarray(img_arr).resize((new_width, new_height), Image.ANTIALIAS))

一个简单的方法,保持约束比率和通过最大宽度/高度。不是最漂亮的,但可以完成工作,而且很容易理解:

def resize(img_path, max_px_size, output_folder):
    with Image.open(img_path) as img:
        width_0, height_0 = img.size
        out_f_name = os.path.split(img_path)[-1]
        out_f_path = os.path.join(output_folder, out_f_name)

        if max((width_0, height_0)) <= max_px_size:
            print('writing {} to disk (no change from original)'.format(out_f_path))
            img.save(out_f_path)
            return

        if width_0 > height_0:
            wpercent = max_px_size / float(width_0)
            hsize = int(float(height_0) * float(wpercent))
            img = img.resize((max_px_size, hsize), Image.ANTIALIAS)
            print('writing {} to disk'.format(out_f_path))
            img.save(out_f_path)
            return

        if width_0 < height_0:
            hpercent = max_px_size / float(height_0)
            wsize = int(float(width_0) * float(hpercent))
            img = img.resize((max_px_size, wsize), Image.ANTIALIAS)
            print('writing {} to disk'.format(out_f_path))
            img.save(out_f_path)
            return

下面是一个python脚本,使用这个函数来运行批量图像调整。

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