python capture desktop as video source 1

python capture desktop as video source

import numpy as np
import cv2
from mss import mss
from PIL import Image

bounding_box = {'top': 100, 'left': 0, 'width': 400, 'height': 300}

sct = mss()

while True:
    sct_img = sct.grab(bounding_box)
    cv2.imshow('screen', np.array(sct_img))

    if (cv2.waitKey(1) & 0xFF) == ord('q'):
        cv2.destroyAllWindows()
        break

Here is what the above code is Doing:
1. We import the mss library and cv2 library.
2. We define the bounding box of the area we want to capture.
3. We create an object called sct.
4. We create a while loop that will continuously grab the screen.
5. We grab the screen using the sct object and store it in a variable called sct_img.
6. We display the image using the cv2 library.
7. We add a break statement that will break the loop if the user presses the q key.

Similar Posts