Python opencv 截取视频图片并保存
生活随笔
收集整理的這篇文章主要介紹了
Python opencv 截取视频图片并保存
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Python opencv 截取視頻圖片并保存
# coding=utf-8 import os import cv2videos_src_path = r".\vedio\\" #視頻地址 frames_save_path = r".\picture\\" #保存圖片地址 width = 810 height = 985 time_interval = 25 #截圖間隔def video2frame(video_src_path, frame_save_path, frame_width, frame_height, interval):"""將視頻按固定間隔讀取寫入圖片:param video_src_path: 視頻存放路徑:param formats: 包含的所有視頻格式:param frame_save_path: 保存路徑:param frame_width: 保存幀寬:param frame_height: 保存幀高:param interval: 保存幀間隔:return: 幀圖片"""videos = os.listdir(video_src_path)for each_video in videos:print("正在讀取視頻:", each_video)each_video_name = each_video[:-4]os.mkdir(frame_save_path + each_video_name)each_video_save_full_path = os.path.join(frame_save_path, each_video_name) + "/"each_video_full_path = os.path.join(video_src_path, each_video)cap = cv2.VideoCapture(each_video_full_path)frame_index = 0frame_count = 0if cap.isOpened():success = Trueelse:success = Falseprint("讀取失敗!")while(success):success, frame = cap.read()if frame_index % interval == 0 and success: # 如路徑下有多個視頻文件時視頻最后一幀報錯因此條件語句中加and successprint("---> 正在讀取第%d幀:" % frame_index, success)resize_frame = cv2.resize(frame, (frame_width, frame_height), interpolation=cv2.INTER_AREA)cv2.imwrite(each_video_save_full_path + "%d.jpg" % frame_count, resize_frame)frame_count += 1frame_index += 1cap.release()def main():""":return:"""video2frame(videos_src_path, frames_save_path, width, height, time_interval)if __name__ == '__main__':main()總結
以上是生活随笔為你收集整理的Python opencv 截取视频图片并保存的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: iview table实现通过rende
- 下一篇: websocket python爬虫_p