Skip to content

Instantly share code, notes, and snippets.

@S1M0N38
Created April 25, 2017 08:09
Show Gist options
  • Save S1M0N38/26224b8b750200ce31d1b388da929928 to your computer and use it in GitHub Desktop.
Save S1M0N38/26224b8b750200ce31d1b388da929928 to your computer and use it in GitHub Desktop.
MSS (written in pure python, working on windows, mac, linux) is capable to grab the screen at ~40 fps at 800x400 resolution.
import numpy as np
import cv2
import time
from mss import mss
from PIL import Image
'''Macbook have a bug about box's dimensions, can be just multiple of 16
but this bug will be fix soon in the next release'''
box = { "top": 10, "left": 10, "width": 400, "height": 320}
sct = mss()
while True:
t = time.time()
sct.get_pixels(box)
image = Image.frombytes('RGB', (sct.width, sct.height), sct.image)
# Could be useful in MAC OS
#b, g, r = image.split()
#image = Image.merge("RGB", (r, g, b))
cv2.imshow('test', np.array(image))
print('fps: {}'.format(1/(time.time()-t)))
if cv2.waitKey(25) & 0xFF == ord('q'):
cv2.destroyAllWindows()
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment