mss python screenshot code example
Example 1: python screen recording mss
import time
import cv2
import mss
import numpy
with mss.mss() as sct:
monitor = {"top": 40, "left": 0, "width": 800, "height": 640}
while "Screen capturing":
last_time = time.time()
img = numpy.array(sct.grab(monitor))
cv2.imshow("OpenCV/Numpy normal", img)
print("fps: {}".format(1 / (time.time() - last_time)))
if cv2.waitKey(25) & 0xFF == ord("q"):
cv2.destroyAllWindows()
break
Example 2: python screen recording mss
import mss
import mss.tools
with mss.mss() as sct:
monitor = {"top": 160, "left": 160, "width": 160, "height": 135}
output = "sct-{top}x{left}_{width}x{height}.png".format(**monitor)
sct_img = sct.grab(monitor)
mss.tools.to_png(sct_img.rgb, sct_img.size, output=output)
print(output)