栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

视频转图片,图片转视频 OpenCV-python实现

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

视频转图片,图片转视频 OpenCV-python实现

处理视频的时候,每次都要写重复的代码,故写篇博客,做个备份,方便ctrl cv;

1、视频转图片

##video_path 为输入的视频路径;
##imgs_path 为输出的图像序列路径;
def video2img(video_path,imgs_path):
    num = 0
    vid = cv2.VideoCapture(video_path)
    fps = vid.get(cv2.CAP_PROP_FPS)
    print('fps is ',fps)
    while vid.isOpened():
        is_read, frame = vid.read()
        print('num is ',num,num%fps)
        if is_read:
            cv2.imwrite(imgs_path + '/'+str('%08d' % num) + '.jpg', frame)
            num += 1
        else:
            break

2、图片转视频

#src_path 为图像序列的路径;
#dst_path 为输出视频的路径;
#默认输出的格式为mp4,输出文件为output.mp4;
def img2video(src_path,dst_path,name = 'output.mp4'):
    # if not os.path.exists(dst_path):
    #     os.makedirs(dst_path, exist_ok=True)
    fourcc = cv2.VideoWriter_fourcc(*'mp4v')
    #图片的尺寸,(width,height)
    img_size = (640, 480)
    fps = 10
    video_writer = cv2.VideoWriter(filename=os.path.join(dst_path, name), fourcc=fourcc, fps=fps,
                                   frameSize=img_size)
    for jpg in os.listdir(src_path):
        if jpg.endswith('.jpg'):
            frame = cv2.imread(os.path.join(src_path,jpg))
            video_writer.write(frame)
        else:
            print(jpg)
    video_writer.release()
    print('process ok!')
    return

有问题欢迎交流!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1026636.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号