python opencv 图片/视频 拼接
# coding: utf-8
# 像写诗一样写代码
import numpy as np
import cv2
img_A_path = "C:\\Pictures\\yy.jpg" # 待合成的 图片A路径
img_B_path = "C:\\Pictures\\yy1.jpg" # 待合成的 图片B路径
video_A_path = "D:\\Pycharm Pro\\o\\video\\3D mesh Advanced.mp4" # 待合成的 视频A路径
video_B_path = "C:\\yuyang2\\桌面\\video\\3D mesh Advanced.mp4" # 待合成的 视频B路径
video_A_show_name = "HumanAction V8.5.0" # 视频A 显示的版本号
video_B_show_name = "HumanAction V8.4.0" # 视频B 显示的版本号
def pic_stitch(imgA, imgB):
"""
合成图片A、B
"""
img_array_A = cv2.imread(imgA)
cv2.putText(img_array_A, video_A_show_name, (50, 100), cv2.FONT_HERSHEY_COMPLEX, 1.5, (100, 200, 200), 3)
img_array_B = cv2.imread(imgB)
cv2.putText(img_array_B, video_A_show_name, (50, 100), cv2.FONT_HERSHEY_COMPLEX, 1.5, (100, 200, 200), 3)
img_array_AB = np.hstack((img_array_A, img_array_B))
cv2.imwrite("pic_stitch.jpg", img_array_AB)
def video_stitch(videoA, videoB):
"""
合成视频A、B
"""
cap_video_a = cv2.VideoCapture(videoA)
cap_video_b = cv2.VideoCapture(videoB)
fps = int(cap_video_a.get(cv2.CAP_PROP_FPS))
size = (int(cap_video_b.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap_video_b.get(cv2.CAP_PROP_FRAME_HEIGHT)))
videoWriter = cv2.VideoWriter('video_stitch.avi', cv2.VideoWriter_fourcc('X', 'V', 'I', 'D'), fps,
(size[0] * 2, size[1]))
ret_a, frame_a = cap_video_a.read()
ret_b, frame_b = cap_video_b.read()
while ret_a and ret_b:
img_array_AB = np.hstack((frame_a, frame_b))
videoWriter.write(img_array_AB)
ret_a, frame_a = cap_video_a.read()
cv2.putText(frame_a, video_A_show_name, (50, 100), cv2.FONT_HERSHEY_COMPLEX, 1.5, (100, 200, 200), 3)
ret_b, frame_b = cap_video_b.read()
cv2.putText(frame_b, video_B_show_name, (50, 100), cv2.FONT_HERSHEY_COMPLEX, 1.5, (100, 200, 200), 3)
if __name__ == "__main__":
# pic_stitch(img_A_path, img_B_path) # 合成图片A、B
video_stitch(video_A_path, video_B_path) # 合成视频A、B