python:图片拼接
一:尺寸相同的图片拼接
import os from PIL import Image width_i = 200 height_i = 200 row_max = 3 line_max = 3 all_path = list() num = 0 pic_max = line_max * row_max dir_name = r"C:\Users\Administrator\Desktop\demopic" # root文件夹的路径 dirs 路径下的文件夹列表 files路径下的文件列表 for root, dirs, files in os.walk(dir_name): for file in files: if "jpg" in file: # 子串在母串里面不 all_path.append(os.path.join(root,file)) # all_path获取每张图片的绝对路径 toImage = Image.new('RGBA',(width_i*line_max,height_i*row_max)) for i in range(row_max): for j in range(line_max): # 每次打开图片绝对路路径列表的第一张图片 pic_fole_head = Image.open(all_path[num]) # 获取图片的尺寸 wihth,height = pic_fole_head.size # 按照指定的尺寸,给图片重新赋值,<PIL.Image.Image image mode=RGB size=200x200 at 0x127B7978> tmppic = pic_fole_head.resize((width_i, height_i)) # 计算每个图片的左上角的坐标点(0, 0),(0, 200),(0, 400),(200, 0),(200, 200)。。。。(400, 400) loc = (int(i % line_max * width_i), int(j % line_max * height_i)) print("第{}张图的存放位置".format(num),loc) toImage.paste(tmppic, loc) num = num + 1 if num >= len(all_path): print("breadk") break if num >= pic_max: break print(toImage.size) toImage.save('merged.png')
![](https://img2018.cnblogs.com/blog/1749518/201909/1749518-20190928204022959-362249681.png)
二:尺寸不相同的图片进行拼接
例如:将这种图,拼接成一幅完整的图