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


当前回答

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

我还将添加一个调整大小的版本,以保持纵横比固定。 在本例中,它将根据初始宽高比asp_rat (float(!))调整高度以匹配新图像的宽度。 但是,要调整宽与高,只需在else循环中注释一行,取消注释另一行。你会看到,在哪里。

你不需要分号(;),我保留它们只是为了提醒自己我经常使用的语言的语法。

from PIL import Image

img_path = "filename.png";
img = Image.open(img_path);     # puts our image to the buffer of the PIL.Image object

width, height = img.size;
asp_rat = width/height;

# Enter new width (in pixels)
new_width = 50;

# Enter new height (in pixels)
new_height = 54;

new_rat = new_width/new_height;

if (new_rat == asp_rat):
    img = img.resize((new_width, new_height), Image.ANTIALIAS); 

# adjusts the height to match the width
# NOTE: if you want to adjust the width to the height, instead -> 
# uncomment the second line (new_width) and comment the first one (new_height)
else:
    new_height = round(new_width / asp_rat);
    #new_width = round(new_height * asp_rat);
    img = img.resize((new_width, new_height), Image.ANTIALIAS);

# usage: resize((x,y), resample)
# resample filter -> PIL.Image.BILINEAR, PIL.Image.NEAREST (default), PIL.Image.BICUBIC, etc..
# https://pillow.readthedocs.io/en/3.1.x/reference/Image.html#PIL.Image.Image.resize

# Enter the name under which you would like to save the new image
img.save("outputname.png");

这样就完成了。我尽我所能记录下来,所以很清楚。

我希望这对外面的人有所帮助!

基于@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)

对我来说最简单的方法

image = image.resize((image.width*2, image.height*2), Image.ANTIALIAS)

例子

from PIL import Image, ImageGrab
image = ImageGrab.grab(bbox=(0,0,400,600)) #take screenshot
image = image.resize((image.width*2, image.height*2), Image.ANTIALIAS)
image.save('Screen.png')

举个丑陋的例子。

函数获取像pic[0-9a-z]这样的文件。[扩展]",将它们调整为120x120,移动部分到中心,并保存到"ico[0-9a-z]。[扩展]”,肖像和风景作品:

def imageResize(filepath):
    from PIL import Image
    file_dir=os.path.split(filepath)
    img = Image.open(filepath)

    if img.size[0] > img.size[1]:
        aspect = img.size[1]/120
        new_size = (img.size[0]/aspect, 120)
    else:
        aspect = img.size[0]/120
        new_size = (120, img.size[1]/aspect)
    img.resize(new_size).save(file_dir[0]+'/ico'+file_dir[1][3:])
    img = Image.open(file_dir[0]+'/ico'+file_dir[1][3:])

    if img.size[0] > img.size[1]:
        new_img = img.crop( (
            (((img.size[0])-120)/2),
            0,
            120+(((img.size[0])-120)/2),
            120
        ) )
    else:
        new_img = img.crop( (
            0,
            (((img.size[1])-120)/2),
            120,
            120+(((img.size[1])-120)/2)
        ) )

    new_img.save(file_dir[0]+'/ico'+file_dir[1][3:])