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 =

# See image numeric values.

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

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


Install OpenCV for Python on macOS

To install the OpenCV library for computer vision on macOS, first install the required prerequisites, then the package itself.

pip install scikit-build

pip install cmake

pip install opencv-python

Test the installation:


>> import cv2

The import should work without errors.