首页 > 编程语言 > 详细

python提取视频中的图片

时间:2020-03-30 13:57:55      阅读:92      评论:0      收藏:0      [点我收藏+]
# 导入所需要的库
import cv2
import numpy as np
 
# 定义保存图片函数
# image:要保存的图片名字
# addr;图片地址与相片名字的前部分
# num: 相片,名字的后缀。int 类型
def save_image(image,addr,num):
    address = addr + str(num)+ .jpg
    cv2.imwrite(address,image)
 
# 读取视频文件
videoCapture = cv2.VideoCapture("2.mp4")#文件地址
# 通过摄像头的方式
# videoCapture=cv2.VideoCapture(1)
 
#读帧
success, frame = videoCapture.read()
i = 0
while success :
    i = i + 1
    save_image(frame,./output/image,i)#图片保存地址
    if success:
        print(save image:,i)
    success, frame = videoCapture.read()

加入帧数

# 导入所需要的库
import cv2
import numpy as np
 
# 定义保存图片函数
# image:要保存的图片名字
# addr;图片地址与相片名字的前部分
# num: 相片,名字的后缀。int 类型
def save_image(image,addr,num):
    address = addr + str(num)+ .jpg
    cv2.imwrite(address,image)
 
# 读取视频文件
videoCapture = cv2.VideoCapture("2.mp4")
# 通过摄像头的方式
# videoCapture=cv2.VideoCapture(1)
 
#读帧
success, frame = videoCapture.read()
i = 0
timeF = 12
j=0
while success :
    i = i + 1
    if (i % timeF == 0):
        j = j + 1
        save_image(frame,./output/image,j)
        print(save image:,i)
    success, frame = videoCapture.read()

提取avi视频

from PIL import Image
import cv2
 
def splitFrames(videoFileName):
    cap = cv2.VideoCapture(videoFileName) # 打开视频文件
    num = 1
    while True:
        # success 表示是否成功,data是当前帧的图像数据;.read读取一帧图像,移动到下一帧
        success, data = cap.read()
        if not success:
            break
        im = Image.fromarray(data) # 重建图像
        im.save(result/mold +str(num)+".jpg") # 保存当前帧的静态图像
        print(num)
        num = num + 1
        
    cap.release()
 
splitFrames(2.avi)

参考链接:https://blog.csdn.net/ningcaichen1997/article/details/86018214

                  https://blog.csdn.net/qianbin3200896/article/details/96858077

python提取视频中的图片

原文:https://www.cnblogs.com/Fiona-Y/p/12598002.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!