python提取视频中的图片

# 导入所需要的库
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