# -*- coding:utf8 -*- import cv2 import os import shutil def get_frame_from_video(video_name, interval): """ Args: video_name:輸入影片名字 interval: 儲存圖片的幀率間隔 Returns: """ # 儲存圖片的路徑 save_path = video_name.split('.MOV')[0] + '/' is_exists = os.path.exists(save_path) if not is_exists: os.makedirs(save_path) print('path of %s is build' % save_path) else: shutil.rmtree(save_path) os.makedirs(save_path) print('path of %s already exist and rebuild' % save_path) # 開始讀影片 video_capture = cv2.VideoCapture(video_name) i = 0 j = 0 while True: success, frame = video_capture.read() i += 1 if i % interval == 0: cv2.imshow("Image", frame) cv2.waitKey (1) # 儲存圖片 j += 1 #save_name = save_path + str(j) + '_' + str(i) + '.jpg' save_name = save_path + str(j) + '.jpg' cv2.imwrite(save_name, frame) print('image of %s is saved' % save_name) if not success: print('video is all read') break if __name__ == '__main__': # 影片檔名字 video_name = '/media/dongdong/新加捲/2Work/3MyWork/1data/3大門外高空資料/80米/DJI_0559.MOV' interval = 20 get_frame_from_video(video_name, interval) cv2.destroyAllWindows()