cv2.read() code example

Example 1: show image opencv python

import cv2
img = cv2.imread('/path_to_image/opencv-logo.png')
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

# to use it in a loop
k = cv2.waitKey(0)
if k == 27:         # wait for ESC key to exit
    cv2.destroyAllWindows()
elif k == ord('s'): # wait for 's' key to save and exit
    cv2.imwrite('messigray.png',img)
    cv2.destroyAllWindows()

Example 2: how to show webcam in opencv

import cv2

cap = cv2.VideoCapture(0)

while True:
    success, img = cap.read()
    cv2.imshow("Webcam", img)
    if cv2.waitKey(1) == ord('q'):
        break