背景
希望能将多张宽高大小不一致的图片,按每组n个合并成一张图片
Show me the code
import os
from PIL import Image
def mergeImages(oriPath,v1Path,v2Path):
imagefile = []
oriImg = None
v1Img = None
v2Img = None
try:
oriImg = Image.open(oriPath).convert('RGB')
v1Img = Image.open(v1Path).convert('RGB')
v2Img = Image.open(v2Path).convert('RGB')
except Exception as e:
return
imagefile.append(oriImg)
imagefile.append(v1Img)
imagefile.append(v2Img)
width = oriImg.size[0] + v1Img.size[0] + v2Img.size[0]
height = max(oriImg.size[1],v1Img.size[1],v2Img.size[1])
target = Image.new('RGB', (width, height))
left = 0
for image in imagefile:
target.paste(image, (left, 0, left + image.size[0], 0 + image.size[1]))
left += image.size[0]
target.save(oriPath.replace("V1","out"), quality = 1000)
def main():
for root,dirs,files in os.walk("/Users/xx/xx"):
for file in files:
if not "_out" in file:
oriPath = os.path.join(root,file)
v1Path = oriPath.replace(".jpg","_out.jpg")
v2Path = v1Path.replace("V1","V2")
mergeImages(oriPath,v1Path,v2Path)
if __name__ == '__main__':
main()
网友评论