2008-11-07 246 views
266

有没有一种明显的方式来做到这一点,我失踪了?我只是想制作缩略图。如何使用PIL调整图像大小并保持其宽高比?

+7

由于这个问题是很老,但有用的,枕头是相当首选,对于基于枕教程看看这个:HTTP ://pillow.readthedocs.org/en/latest/handbook/tutorial.html#create-jpeg-thumbnails – Wtower 2015-03-26 10:27:53

+1

我创建了一个用于调整图像大小的小型库,它可以提供任何帮助:https://github.com/ charlesthk/python-resize-image – Charlesthk 2015-04-28 14:49:12

回答

326

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

正确的尺寸是oldsize*ratio

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

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.ANTIALIAS) 
      im.save(outfile, "JPEG") 
     except IOError: 
      print "cannot create thumbnail for '%s'" % infile 
+58

** [Randy Syring]评论(http://stackoverflow.com/users/182111/randy-syring)**: 将`Image.ANTIALIAS`添加到`thumbnail()`调用。这是强烈建议在文档中,这是最好的答案,应遵循最佳实践。换句话说:用`im.thumbnail(size,Image.ANTIALIAS)`替换`im.thumbnail(size)`。 – Anne 2011-12-07 21:50:40

+2

就像它说的那样,这个例子来自pil文档,那个例子(仍然)不使用antialias标志。因为这可能是大多数人想要的,但是,我添加了它。 – gnud 2011-12-08 08:29:50

+2

PIL将新图像的高度设置为给定的尺寸(此处为128),并计算宽度以保持纵横比。有没有办法修复宽度而不是高度?也许我会在单独的问题中提出这个问题。 – eugene 2012-12-13 08:22:32

9

如果您试图保持相同的宽高比,那么您是否不会调整原始大小的某个百分比?

例如,原来的一半大小

half = 0.5 
out = im.resize([int(half * s) for s in im.size]) 
136

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

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.ANTIALIAS) 
img.save('sompic.jpg') 
46

我还推荐使用PIL的缩略图方法,因为它可以消除所有的比例麻烦。

一个重要的暗示,虽然:在默认情况下

im.thumbnail(size,Image.ANTIALIAS) 

更换

im.thumbnail(size) 

,PIL使用Image.NEAREST过滤器的大小调整导致性能好,但质量较差。

13

PIL已经裁剪图像

img = ImageOps.fit(img, size, Image.ANTIALIAS) 
1

我的丑例子的选项。

函数获取如下文件:“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:]) 
17

基于在@tomvon,我使用以下完成:

调整大小宽度:

new_width = 680 
new_height = new_width * height/width 

大小调整高度:

new_height = 680 
new_width = new_height * width/height 

然后只是:

img = img.resize((new_width, new_height), Image.ANTIALIAS) 
2
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 
8
from PIL import Image 

img = Image.open('/your iamge 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 u want ,*.png,*jpg,*.gif 
相关问题