import os import cv2 import numpy as np import time path = './new_image/' filelist = os.listdir(path) fps = 0.5 # 视频每秒0.5帧 # 每张图片的尺寸必须是一样的 size = (512, 512) # opencv读取的视频只能是avi格式 video = cv2.VideoWriter("VideoTest1.avi", cv2.VideoWriter_fourcc("M", "J","P","G"), fps, size) # 视频保存在当前目录下 for item in filelist: if item.endswith('.jpg'): # 找到路径中所有后缀名为.png的文件,可以更换为.jpg或其它 item = path + item img = cv2.imread(item) video.write(img) time.sleep(2) print("#" * 100)
# 读取压缩完的视频 cap_cramer = cv2.VideoCapture("./VideoTest1.avi") while True: ret, frame = cap_cramer.read() print("ret......", ret) print(frame) if ret is False: break cv2.imshow("frame", frame) cv2.waitKey(2000)