1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
import os from PIL import Image unitW = 800 unitH = 800 unitPath = "./原始图/" targetPath = "./拼接后/" targetNamePath = "文件名列表.txt"
unitName = [] targetName = [] for root, dirs, files in os.walk(unitPath): for f in files : unitName.append(f) print("共导入", len(unitName), "张图片。") with open(targetNamePath,'r', encoding='UTF-8') as text: for line in text: targetName.append(line.strip('\n')) print("开始拼接图片……") for i in range(len(unitName)//4): unitFile = [] for j in range(4): unitFile.append(Image.open(unitPath + unitName[i*4+j])) target = Image.new('RGB', (2 * unitW, 2 * unitH)) for k in range(len(unitFile)): if k == 0: target.paste(unitFile[k], (0,0)) elif k == 1: target.paste(unitFile[k], (unitW,0)) elif k == 2: target.paste(unitFile[k], (0,unitH)) else: target.paste(unitFile[k], (unitW,unitH)) target.save(targetPath+str(i+1).zfill(2)+'.'+targetName[i]+'.png', quality = 100) unitFile = [] print("图片拼接完成。")
|