opencv图像转视频
import os, json, glob
from PIL import Image
import cv2
import numpy as np
def img2video(src_path, outputname="video.avi"):
file_list = [x for x in glob.glob(os.path.join(src_path, "*"))]
file_list.sort()
img_src = Image.open(file_list[0])
fourcc = cv2.VideoWriter_fourcc(*'XVID')
videoWriter = cv2.VideoWriter(outputname, fourcc, 30, (img_src.width, img_src.height))
for filename in file_list:
img = cv2.imread(filename)
videoWriter.write(img)
videoWriter.release()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
左右拼接图像转视频
如果要左右拼接图像,并转为视频,可以使用如下代码片段
from PIL import Image
a_img = Image.open(filename)
b_img = Image.open(dstfile)
dst = Image.new('RGB', (a_img.width * 2, a_img.height))
dst.paste(a_img, (0, 0))
dst.paste(b_img, (a_img.width, 0))
videoWriter.write(cv2.cvtColor(np.array(dst), cv2.COLOR_RGB2BGR))
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9