Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Webcam capture for python - Pops up a preview from your webcam and snaps a picture when you press 'q'
import cv2
# Windows dependencies
# - Python 2.7.6:
# - OpenCV:
# - Numpy -- get numpy from here because the official builds don't support x64:
# Mac Dependencies
# - brew install python
# - pip install numpy
# - brew tap homebrew/science
# - brew install opencv
cap = cv2.VideoCapture(0)
ret, frame =
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA)
cv2.imshow('frame', rgb)
if cv2.waitKey(1) & 0xFF == ord('q'):
out = cv2.imwrite('capture.jpg', frame)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment