问题描述: 将16张图片拼接成4x4的图片, 且不改变每单张图片的像素值.
代码如下:
1 import os 2 from PIL import Image 3 path = '/share1/home/cong/Datasets/resized_Polyvore/pair1/pair1' 4 imglist = os.listdir(path) 5 imglist.sort() 6 imglist = imglist[:16] 7 images = [] 8 for item in imglist: 9 images.append(Image.open(path + '/' + item)) 10 11 h, w = 128, 128 12 target = Image.new('RGBA', (w * 4, h * 4)) 13 for idx, img in enumerate(images): 14 i = idx % 4 15 j = idx // 4 16 target.paste(img, (i * w, j * h, i * w + w, j * h + h)) 17 18 target.save('.' + '/' + 'real.png')
效果如下: