Python PIL 圖像縮小、拼接


比較各種不同取樣方式的圖像縮放效果。

[NEAREST, BILINEAR, BICUBIC, LANCZOS, BOX, HAMMING]
NEAREST取樣方式是效果最差的,PIL.Image.resize默認的resample方式就是使用NEAREST
import os
from PIL import Image
from PIL.Image import NEAREST, BILINEAR, BICUBIC, LANCZOS, BOX, HAMMING

resmaple_list = [NEAREST, BILINEAR, BICUBIC, LANCZOS, BOX, HAMMING]
path = r'C:\Users\UserName\Desktop\Wallpapers'

filename = 'Biosphere Montreal.jpg'
fullname = os.path.join(path, filename)

image = Image.open(fullname)
cut_size = [int(x*0.1) for x in image.size]
new_size = (cut_size[0]*3, cut_size[1]*2)

new_image = Image.new('RGB', new_size)
for i in range(0, 3):
    im = image.resize(cut_size, resample=resmaple_list[i])
    new_image.paste(im, (i*cut_size[0], 0))

for i in range(3, 6):
    im = image.resize(cut_size, resample=resmaple_list[i])
    new_image.paste(im, ((i-3)*cut_size[0], cut_size[1]))

new_image.show()
new_image.save(os.path.join(path, 'outputImage.jpg'))

  

結果圖:

原圖左上角細節如下:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM