Python-opencv实现视频与图片的相互转换

https://blog.csdn.net/hitzijiyingcai/article/details/86668923
视频转图片

import cv2
vc=cv2.VideoCapture("/home/hqd/PycharmProjects/1/1/19.MOV")
c=1
if vc.isOpened():
    rval,frame=vc.read()
else:
    rval=False
while rval:
    rval,frame=vc.read()
    cv2.imwrite('/home/hqd/PycharmProjects/1/1/19/'+str(c)+'.jpg',frame)
    c=c+1
    cv2.waitKey(1)
vc.release()

图片转视频

import cv2
import os
 
fps = 30
fourcc = cv2.VideoWriter_fourcc('M', 'J', 'P', 'G')
video_writer = cv2.VideoWriter(filename='./result.avi', fourcc=fourcc, fps=fps, frameSize=(1920, 1080))
for i in range(0,6000):
    p = i
    if os.path.exists('/home/hqd/PycharmProjects/1/qqqq/'+str(p)+'.jpg'):    #判断图片是否存在
        img = cv2.imread(filename='/home/hqd/PycharmProjects/1/qqqq/'+str(p)+'.jpg')
        cv2.waitKey(100)
        video_writer.write(img)
        print(str(p) + '.jpg' + ' done!')
video_writer.release()

你可能感兴趣的:(Python-opencv实现视频与图片的相互转换)