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


当前回答

只是用更现代的包装器更新了这个问题 这个图书馆包枕头(PIL的一个叉子) https://pypi.org/project/python-resize-image/

允许你这样做:-

from PIL import Image
from resizeimage import resizeimage

fd_img = open('test-image.jpeg', 'r')
img = Image.open(fd_img)
img = resizeimage.resize_width(img, 200)
img.save('test-image-width.jpeg', img.format)
fd_img.close()

在上面的链接中有更多的例子。

其他回答

要使新图像的宽度和高度是原图像的一半,请使用以下代码:

  from PIL import Image
  im = Image.open("image.jpg")
  resized_im = im.resize((round(im.size[0]*0.5), round(im.size[1]*0.5)))
    
  #Save the cropped image
  resized_im.save('resizedimage.jpg')

调整:用定额调整固定宽度:

from PIL import Image
new_width = 300
im = Image.open("img/7.jpeg")
concat = int(new_width/float(im.size[0]))
size = int((float(im.size[1])*float(concat)))
resized_im = im.resize((new_width,size), Image.ANTIALIAS)
#Save the cropped image
resized_im.save('resizedimage.jpg')
# Importing Image class from PIL module
from PIL import Image

# Opens a image in RGB mode
im = Image.open(r"C:\Users\System-Pc\Desktop\ybear.jpg")

# Size of the image in pixels (size of original image)
# (This is not mandatory)
width, height = im.size

# Setting the points for cropped image
left = 4
top = height / 5
right = 154
bottom = 3 * height / 5

# Cropped image of above dimension
# (It will not change original image)
im1 = im.crop((left, top, right, bottom))
newsize = (300, 300)
im1 = im1.resize(newsize)
# Shows the image in image viewer
im1.show()

定义最大大小。 然后,通过取min(maxwidth/width, maxheight/height)来计算调整大小的比率。

合适的尺寸是旧尺寸*比例。

当然,还有一个库方法可以做到这一点:Image.thumbnail方法。 下面是来自PIL文档的一个(编辑过的)示例。

import os, sys
import Image

size = 128, 128

for infile in sys.argv[1:]:
    outfile = os.path.splitext(infile)[0] + ".thumbnail"
    if infile != outfile:
        try:
            im = Image.open(infile)
            im.thumbnail(size, Image.Resampling.LANCZOS)
            im.save(outfile, "JPEG")
        except IOError:
            print "cannot create thumbnail for '%s'" % infile

我还将添加一个调整大小的版本,以保持纵横比固定。 在本例中,它将根据初始宽高比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");

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

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

打开图像文件

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

等等