python opencv screen capture code example
Example 1: python cv2 screen capture
import numpy as np
import cv2
from PIL import ImageGrab
while True:
img = ImageGrab.grab(bbox=(0, 1000, 100, 1100))
img_np = np.array(img)
frame = cv2.cvtColor(img_np, cv2.COLOR_BGR2GRAY)
cv2.imshow("frame", frame)
if cv2.waitKey(1) & 0Xff == ord('q'):
break
cv2.destroyAllWindows()
Example 2: python opencv camera capture
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while(True):
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()