2008-11-07 244 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 
相關問題