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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM