有没有我忽略的明显的方法?我只是想做个缩略图。
当前回答
打开图像文件
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) )
等等
其他回答
下面的脚本创建所有JPEG图像的漂亮缩略图,保留128x128最大分辨率的纵横比。
from PIL import Image
img = Image.open("D:\\Pictures\\John.jpg")
img.thumbnail((680,680))
img.save("D:\\Pictures\\John_resize.jpg")
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
我还推荐使用PIL的缩略图方法,因为它消除了所有的比例麻烦。
不过,有一个重要的提示:替换
im.thumbnail(size)
with
im.thumbnail(size,Image.ANTIALIAS)
默认情况下,PIL使用映像。最近的过滤器调整大小,结果良好的性能,但质量差。
我还将添加一个调整大小的版本,以保持纵横比固定。 在本例中,它将根据初始宽高比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");
这样就完成了。我尽我所能记录下来,所以很清楚。
我希望这对外面的人有所帮助!
举个丑陋的例子。
函数获取像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:])