python实现单张图像拼接与批量图片拼接

本文实例为大家分享了python实现图像拼接的具体代码,供大家参考,具体内容如下

一、效果

二、代码

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实现单张图像拼接与批量图片拼接)