欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

opencv-python视频处理之图片变成视频,视频切成图片

程序员文章站 2022-03-07 20:18:37
如果视频打开成功每隔100帧数保存一个图片...
import cv2
def video2image():

    vc= cv2.VideoCapture('test.avi' )
    c=1
    # 如果视频打开成功每隔100帧数保存一个图片
    if vc.isOpened() :
        rval, frame = vc.read ()
    else:
        rval = False
        timeF = 1000
    while rval :
        rval, frame = vc. read ()
        if c%100==0:
            cv2.imwrite (' image/' + str(c) + '. jpg', frame)
        c=c+ 1
        cv2.waitKey (1)
    vc. release ()


















import glob
import os
import cv2
def images2video(image_dir,save_name):
    fps=24
    fourcc=cv2.VideoWriter_fourcc(*"MJPG")
    video_w=cv2.VideoWriter(save_name,fourcc,fps,(640,480))
    # 扫描文件夹中所有jpg文件
    images=glob.glob(os.path.join(image_dir,"*.jpg"))
    for i in range(len(images)):
        # 选中名为"image_{:03d}.jpg".format(1) =image_001.jpg
        image_name=os.path.join(image_dir,"image_{:03d}.jpg".format(i))
        frame=cv2.imread(image_name)
        video_w.write(frame)
        video_w.release()
        
        






本文地址:https://blog.csdn.net/weixin_32759777/article/details/107296479