需求:一个文件夹内有若干图片,可以被10整除,希望每10张图片横向拼接成一张大图。
import os
from PIL import Image
UNIT_SIZE = 96 # 单个图像的大小长度为96
TARGET_WIDTH = 10 * UNIT_SIZE # 拼接完后的横向长度为10*96
path = "C:/Users/Administrator/Desktop/bbbb"
save_path = 'C:/Users/Administrator/Desktop/bbbbb/'
images = [] # 存储图像的名称的列表
for root, dirs, files in os.walk(path):
for f in files :
images.append(f)
print('images=',images)
for i in range(len(images)/10): # 10个图像为一组
imagefile = []
j = 0
for j in range(10):
imagefile.append(Image.open(path+'/'+images[i*10+j]))
target = Image.new('L', (TARGET_WIDTH, UNIT_SIZE))
left = 0
right = UNIT_SIZE
for image in imagefile:
target.paste(image, (left, 0, right, UNIT_SIZE))# 将image复制到target的指定位置中
left += UNIT_SIZE # left是左上角的横坐标,依次递增
right += UNIT_SIZE # right是右下的横坐标,依次递增
quality_value = 1000 # quality来指定生成图片的质量
target.save(save_path+os.pat