Skip to content

Instantly share code, notes, and snippets.

@maderix
Created April 18, 2020 11:36
Show Gist options
  • Select an option

  • Save maderix/8fbd52cc05b1e97da671c6d6d9a44508 to your computer and use it in GitHub Desktop.

Select an option

Save maderix/8fbd52cc05b1e97da671c6d6d9a44508 to your computer and use it in GitHub Desktop.
Get frames from video file, crop and save them
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