本文实例为大家分享了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博客。
标签:numpy
Powered By python教程网 鲁ICP备18013710号
python博客 - 小白学python最友好的网站!