1import time
2
3import cv2
4import mss
5import numpy
6
7
8with mss.mss() as sct:
9 # Part of the screen to capture
10 monitor = {"top": 40, "left": 0, "width": 800, "height": 640}
11
12 while "Screen capturing":
13 last_time = time.time()
14
15 # Get raw pixels from the screen, save it to a Numpy array
16 img = numpy.array(sct.grab(monitor))
17
18 # Display the picture
19 cv2.imshow("OpenCV/Numpy normal", img)
20
21 # Display the picture in grayscale
22 # cv2.imshow('OpenCV/Numpy grayscale',
23 # cv2.cvtColor(img, cv2.COLOR_BGRA2GRAY))
24
25 print("fps: {}".format(1 / (time.time() - last_time)))
26
27 # Press "q" to quit
28 if cv2.waitKey(25) & 0xFF == ord("q"):
29 cv2.destroyAllWindows()
30 break
31
1import mss
2import mss.tools
3
4
5with mss.mss() as sct:
6 # The screen part to capture
7 monitor = {"top": 160, "left": 160, "width": 160, "height": 135}
8 output = "sct-{top}x{left}_{width}x{height}.png".format(**monitor)
9
10 # Grab the data
11 sct_img = sct.grab(monitor)
12
13 # Save to the picture file
14 mss.tools.to_png(sct_img.rgb, sct_img.size, output=output)
15 print(output)
16