通过OpenCV将视频分解成图像:
import cv2
import os
video = cv2.VideoCapture(r'F:\SuperResolution\BasicVSR_PlusPlus\data\test_video.mp4')
num = 0
while True:
ret, frame = video.read() # 读取视频帧
if not ret:
break
frame = cv2.resize(frame, (1280, 720), interpolation=cv2.INTER_CUBIC) # 调整图像大小
cv2.imwrite(os.path.join(r'F:\SuperResolution\BasicVSR_PlusPlus\data\demo_000', '{}'.format(num).zfill(8)+'.png'), frame) # 保存图像
num += 1
video.release()
cv2.destroyAllWindows()
通过OpenCV将图像合并成视频:
import cv2
import os
save_path = r'F:/SuperResolution/BasicVSR_PlusPlus/results/demo.avi'
fourcc = cv2.VideoWriter_fourcc(*'XVID')
fps = 24.0
out = cv2.VideoWriter(save_path, fourcc, fps, (1280, 720)) # 创建视频写入对象
path = r'F:\SuperResolution\BasicVSR_PlusPlus\results\demo_111' # 存储图像的目录
lst = os.listdir(path) # 图像名称列表
for name in lst:
img_path = os.path.join(path, name) # 图像路径
frame = cv2.imread(img_path) # 读取图像
out.write(frame) # 将图像写入视频
cv2.destroyAllWindows()