Created
April 18, 2020 11:36
-
-
Save maderix/8fbd52cc05b1e97da671c6d6d9a44508 to your computer and use it in GitHub Desktop.
Get frames from video file, crop and save them
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import cv2 | |
| cap = cv2.VideoCapture('video.mp4') | |
| count = 0 | |
| while cap.isOpened(): | |
| ret, frame = cap.read() | |
| if ret: | |
| h,w = frame.shape[0],frame.shape[1] | |
| if h > w: | |
| frame = frame[:w,:w,:] | |
| else: | |
| frame = frame[:h,:h,:] | |
| frame = cv2.resize(frame,(512,512)) | |
| cv2.imwrite('frame{:d}.jpg'.format(count), frame) | |
| print('writing ','frame{:d}.jpg'.format(count)) | |
| count += 1 # i.e. at 30 fps, this advances one second | |
| cap.set(1, count) | |
| else: | |
| cap.release() | |
| break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment