There are two libraries you can use: OpenCV and ImageIO . Which one to choose is situation-dependent and it is usually best to use the one you are already more familiar with. If you are new to both then ImageIO is easier to learn, so it could be a good starting point. Whichever one you choose, you can find examples for both below:
ImageIO
Installation:
pip install imageio[ffmpeg]
Usage:
Reading frames from a webcam and saving them as images:
Python3
import imageio.v3 as iio
for frame_count, frame in enumerate(iio.imiter("<video0>")):
iio.imwrite(f"frame_{frame_count}.jpg", frame)
if frame_count > 10:
break
Reading frames from a video and saving them as images:
Python3
import imageio.v3 as iio
for frame_count, frame in enumerate(iio.imiter("path/to/video.mp4")):
iio.imwrite(f"frame_{frame_count}.jpg", frame)
OpenCV
Installation:
pip install opencv-python
Usage:
Reading frames from a webcam and saving them as images:
Python3
import cv2
cap = cv2.VideoCapture(0)
i = 0
while(cap.isOpened()):
ret, frame = cap.read()
if ret == False:
break
cv2.imwrite('Frame'+str(i)+'.jpg', frame)
i += 1
cap.release()
cv2.destroyAllWindows()
Output:
Reading frames from a video and saving them as images:
Python3
import cv2
cap = cv2.VideoCapture("path/to/video.mp4")
success, image = cap.read()
frame_count = 0
while success:
cv2.imwrite(f"extracted_images/frame_{frame_count}.jpg", image)
success, image = cap.read()
frame_count += 1
cap.release()
Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills, become a part of the hottest trend in the 21st century.Dive into the future of technology - explore the
Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
03 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Add Your Comment
Please Login to comment...