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

  

结果图:

原图左上角细节如下:

猜你喜欢

转载自www.cnblogs.com/zhangwei22/p/10171622.html