This article is an example of python to share the specific code for image Mosaic, for your reference, the specific content is as follows

A, effects,

Second, the code

1. Single picture stitching

# Image stitching
from PIL import Image
Pil paste can be used for image stitching
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)
 
 # transverse
 img_out = np.concatenate((img_out, img_tmp), axis=1)
 
 # longitudinal
 # img_out = np.concatenate((img_out, img_tmp))
 
cv2.imshow("IMG",img_out)
cv2.imwrite("F:/out/merge.jpg",img_out)
cv2.waitKey(0)
Copy the code

2. Batch image stitching

# Image stitching
from PIL import Image
Pil paste can be used for image stitching
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)
 
# Batch images
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)
 
 # transverse
 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)
Copy the code