python图像拼接 python实现单张图像拼接与批量图片拼接
技术_李艳宾 人气:0一、效果
二、代码
1、单张图片拼接
# 图片拼接 from PIL import Image # pil paste可以进行图片拼接 import cv2 import numpy as np path="F:/out/"+str(0)+".jpg" img_out=cv2.imread(path) num=5 for i in range(1,num): path="F:/out/"+str(i)+".jpg" img_tmp=cv2.imread(path) #横向 img_out = np.concatenate((img_out, img_tmp), axis=1) # 纵向 # img_out = np.concatenate((img_out, img_tmp)) cv2.imshow("IMG",img_out) cv2.imwrite("F:/out/merge.jpg",img_out) cv2.waitKey(0)
2、批量图片拼接
# 图片拼接 from PIL import Image # pil paste可以进行图片拼接 import cv2 import numpy as np import glob as glob import os num=5 os.chdir(r"F:\out") img_name=[] for file_name in glob.glob("*.jpg"): print(file_name) img_name.append(file_name) # 批量处理图片 img_path = glob.glob("F:/out/*jpg") for i in range(int(len(img_path)/num)): path = img_path[i*num] print(path) img_out = cv2.imread(path) for j in range(1, num): path = img_path[i*num+j] img_tmp = cv2.imread(path) # 横向 img_out = np.concatenate((img_out, img_tmp), axis=1) # cv2.imshow("IMG", img_out) cv2.imwrite("F:/out/"+img_name[i*num+j][0:-6]+"_out.jpg", img_out)
本文已被收录到专题《python图片处理操作》 ,欢迎大家点击学习更多精彩内容。
加载全部内容