Capture Webcam Images with Python and OpenCV

The following code shows how to read a video frame from the webcam.

This assumes the OpenCV library for computer vision is installed. See the post on Installing OpenCV on macOS if needed.

To capture one image:

import cv2
import matplotlib.pyplot as pyplot

captureObject = cv2.VideoCapture(0)

success, testImage = captureObject.read()

# See image numeric values.
print(testImage)

# Plot image.
pyplot.imshow(testImage, cmap='gray')
pyplot.show()

To capture many images continuously, we just need to call read() in a loop.

 

Leave a Reply

Your email address will not be published. Required fields are marked *