日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

小程序收集箱:提取视频中的图片(python)

發(fā)布時間:2024/1/18 python 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 小程序收集箱:提取视频中的图片(python) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

本程序用于提取視頻中的圖片。本程序由其他博主提供,在此表示感謝。
如有疑問可聯(lián)系hardenqiu@foxmail.com。

# -*- coding: utf-8 -*- import os import cv2 ##加載OpenCV模塊def video2frames(pathIn='', pathOut='', only_output_video_info = False, extract_time_points = None, initial_extract_time = 0,end_extract_time = None,extract_time_interval = -1, output_prefix = 'frame',jpg_quality = 100,isColor = True):'''pathIn:視頻的路徑,比如:F:\python_tutorials\test.mp4pathOut:設定提取的圖片保存在哪個文件夾下,比如:F:\python_tutorials\frames1\。如果該文件夾不存在,函數(shù)將自動創(chuàng)建它only_output_video_info:如果為True,只輸出視頻信息(長度、幀數(shù)和幀率),不提取圖片extract_time_points:提取的時間點,單位為秒,為元組數(shù)據(jù),比如,(2, 3, 5)表示只提取視頻第2秒, 第3秒,第5秒圖片initial_extract_time:提取的起始時刻,單位為秒,默認為0(即從視頻最開始提取)end_extract_time:提取的終止時刻,單位為秒,默認為None(即視頻終點)extract_time_interval:提取的時間間隔,單位為秒,默認為-1(即輸出時間范圍內的所有幀)output_prefix:圖片的前綴名,默認為frame,圖片的名稱將為frame_000001.jpg、frame_000002.jpg、frame_000003.jpg......jpg_quality:設置圖片質量,范圍為0100,默認為100(質量最佳)isColor:如果為False,輸出的將是黑白圖片'''cap = cv2.VideoCapture(pathIn) ##打開視頻文件n_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) ##視頻的幀數(shù)fps = cap.get(cv2.CAP_PROP_FPS) ##視頻的幀數(shù)dur = n_frames/fps ##視頻的時間##如果only_output_video_info=True, 只輸出視頻信息,不提取圖片if only_output_video_info:print('only output the video information (without extract frames)::::::')print("Duration of the video: {} seconds".format(dur))print("Number of frames: {}".format(n_frames))print("Frames per second (FPS): {}".format(fps)) ##提取特定時間點圖片elif extract_time_points is not None:if max(extract_time_points) > dur: ##判斷時間點是否符合要求raise NameError('the max time point is larger than the video duration....')try:os.mkdir(pathOut)except OSError:passsuccess = Truecount = 0while success and count < len(extract_time_points):cap.set(cv2.CAP_PROP_POS_MSEC, (1000*extract_time_points[count])) success,image = cap.read()if success:if not isColor:image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) ##轉化為黑白圖片print('Write a new frame: {}, {}th'.format(success, count+1))cv2.imwrite(os.path.join(pathOut, "{}_{:06d}.jpg".format(output_prefix, count+1)), image, [int(cv2.IMWRITE_JPEG_QUALITY), jpg_quality]) # save frame as JPEG filecount = count + 1else:##判斷起始時間、終止時間參數(shù)是否符合要求if initial_extract_time > dur:raise NameError('initial extract time is larger than the video duration....')if end_extract_time is not None:if end_extract_time > dur:raise NameError('end extract time is larger than the video duration....')if initial_extract_time > end_extract_time:raise NameError('end extract time is less than the initial extract time....')##時間范圍內的每幀圖片都輸出if extract_time_interval == -1:if initial_extract_time > 0:cap.set(cv2.CAP_PROP_POS_MSEC, (1000*initial_extract_time)) try:os.mkdir(pathOut)except OSError:passprint('Converting a video into frames......')if end_extract_time is not None:N = (end_extract_time - initial_extract_time)*fps + 1success = Truecount = 0while success and count < N:success,image = cap.read()if success:if not isColor:image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)print('Write a new frame: {}, {}/{}'.format(success, count+1, n_frames))cv2.imwrite(os.path.join(pathOut, "{}_{:06d}.jpg".format(output_prefix, count+1)), image, [int(cv2.IMWRITE_JPEG_QUALITY), jpg_quality]) # save frame as JPEG filecount = count + 1else:success = Truecount = 0while success:success,image = cap.read()if success:if not isColor:image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)print('Write a new frame: {}, {}/{}'.format(success, count+1, n_frames))cv2.imwrite(os.path.join(pathOut, "{}_{:06d}.jpg".format(output_prefix, count+1)), image, [int(cv2.IMWRITE_JPEG_QUALITY), jpg_quality]) # save frame as JPEG filecount = count + 1##判斷提取時間間隔設置是否符合要求 elif extract_time_interval > 0 and extract_time_interval < 1/fps:raise NameError('extract_time_interval is less than the frame time interval....')elif extract_time_interval > (n_frames/fps):raise NameError('extract_time_interval is larger than the duration of the video....')##時間范圍內每隔一段時間輸出一張圖片else:try:os.mkdir(pathOut)except OSError:passprint('Converting a video into frames......')if end_extract_time is not None:N = (end_extract_time - initial_extract_time)/extract_time_interval + 1success = Truecount = 0while success and count < N:cap.set(cv2.CAP_PROP_POS_MSEC, (1000*initial_extract_time+count*1000*extract_time_interval)) success,image = cap.read()if success:if not isColor:image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)print('Write a new frame: {}, {}th'.format(success, count+1))cv2.imwrite(os.path.join(pathOut, "{}_{:06d}.jpg".format(output_prefix, count+1)), image, [int(cv2.IMWRITE_JPEG_QUALITY), jpg_quality]) # save frame as JPEG filecount = count + 1else:success = Truecount = 0while success:cap.set(cv2.CAP_PROP_POS_MSEC, (1000*initial_extract_time+count*1000*extract_time_interval)) success,image = cap.read()if success:if not isColor:image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)print('Write a new frame: {}, {}th'.format(success, count+1))cv2.imwrite(os.path.join(pathOut, "{}_{:06d}.jpg".format(output_prefix, count+1)), image, [int(cv2.IMWRITE_JPEG_QUALITY), jpg_quality]) # save frame as JPEG filecount = count + 1##### 測試 pathIn = 'test.mp4' video2frames(pathIn, only_output_video_info = True)pathOut = './frames1/' video2frames(pathIn, pathOut)pathOut = './frames2' video2frames(pathIn, pathOut, extract_time_points=(1, 2, 5))pathOut = './frames3' video2frames(pathIn, pathOut,initial_extract_time=1,end_extract_time=3,extract_time_interval = 0.5) pathOut = './frames4/' video2frames(pathIn, pathOut, extract_time_points=(0.3, 2), isColor = False)pathOut = './frames5/' video2frames(pathIn, pathOut, extract_time_points=(0.3, 2), jpg_quality=50)

總結

以上是生活随笔為你收集整理的小程序收集箱:提取视频中的图片(python)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 超碰这里只有精品 | 丝袜人妖 | 免费毛片看片 | 成人精品免费看 | 涩涩视频在线观看免费 | 浮力影院草草 | 国产无人区码熟妇毛片多 | 国产成人精品一区二区三区四区 | 久久久久久久久久久福利 | 国产一区不卡 | 51精品国产人成在线观看 | 欧美人狂配大交3d | 亚洲大尺度视频 | 久月婷婷 | 重囗味sm一区二区三区 | 久久久久久久久久久久Av | 制服丝袜第一页在线观看 | 91丨porny丨成人蝌蚪 | 日韩成人在线看 | 日韩在线播放一区 | av中文字幕亚洲 | 日日精品| 亚洲午夜剧场 | 中文字幕久久av | 亚洲av永久无码精品国产精品 | 亚洲精品66 | 国产日韩一区二区在线 | 日本高清有码 | 国产精品一区二区在线免费观看 | 日韩精品国产AV | 女人的天堂av | 欧美性爱精品一区 | 亚洲高清视频免费观看 | 综合久久色 | av网站在线免费播放 | 波多一区 | 日韩国产欧美在线视频 | 干日本少妇首页 | 国产精品免费久久久久 | 久久久久久网 | 中文字幕少妇在线三级hd | 国产精品白嫩白嫩大学美女 | 清纯粉嫩极品夜夜嗨av | 偷偷操不一样的久久 | 艹男人的日日夜夜 | 亚洲永久免费精品 | 毛片大全免费 | 五月天狠狠干 | 日韩欧美一区二区三区 | 麻豆传媒在线 | 黄色一级一片免费播放 | 青草一区二区 | 国产亚洲色婷婷久久99精品91 | 精品精品精品 | 国产精品无码电影在线观看 | 夜夜狠狠擅视频 | 精品久久中文字幕 | 国产第9页 | 误杀1电影免费观看高清完整版 | 成人毛片18女人 | 欧美私人网站 | 日日碰狠狠添天天爽无码 | www日本色 | 免费观看成年人视频 | 国产一区二区久久精品 | 免费视频毛片 | 女人被狂躁c到高潮 | 精品99久久 | 午夜久久久精品 | 色婷婷中文字幕 | 精品人妻码一区二区三区红楼视频 | 四虎国产| 国产人妖ts重口系列网站观看 | 欧美伦理片网站 | 色哟哟视频网站 | 国产精品香蕉国产 | 91淫黄大片 | www 在线观看视频 | 欧美激情一级精品国产 | 国产精品99久久久精品无码 | 在线观看成年人网站 | 日本免费色视频 | 岛国av大片 | 最新国产在线视频 | 女生和男生一起插插插 | 国产jizz | 免费国产a级片 | 成人小说亚洲一区二区三区 | 日韩成年视频 | 国产91在线视频 | 无码人妻精品一区二区蜜桃视频 | av免费看片 | 少妇免费视频 | 国产主播喷水 | 黑人巨大精品一区二区在线 | 国产欧美专区 | 女人的洗澡毛片毛多 | 亚洲人丰满奶水 | 欧美中文字幕在线播放 |