python 3.6, Windows 10: I am trying to take one (partial) screenshot every 1-5 milliseconds to then run some custom OCR on it to extract some data. My code for taking the screenshots using the mss package takes between 16 and 47ms depending upon the number of pixels I try to capture.
I have 3 separate lines of questions:
1.) Is there an alternative to mss that is faster?
2.) Is there a way to speed up mss by factor 2-3?
3.) How can I find out via code profiling/cProfile output shown below how I can achieve performance improvements? The way I read the output is that a lot of time is spent in the "grab" function, but it's unclear what inside the grab function actually takes so long.
from mss import mss
import mss.tools as mss_tools
import cProfile, pstats, io
def profile(fnc):
def inner(*args, **kwargs):
pr = cProfile.Profile()
pr.enable()
retval = fnc(*args, **kwargs)
pr.disable()
s = io.StringIO()
sortby = 'cumulative'
ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
ps.print_stats()
print(s.getvalue())
return retval
return inner
@profile
def main():
with mss() as sct:
for i in range(100):
monitor = sct.monitors[1]
left = monitor["left"]
top = monitor["top"]
right = left + 1
lower = top + 1
bbox = (left, top, right, lower)
shot = sct.grab(bbox)
# mss_tools.to_png(shot.rgb, shot.size, output='partialscreen.png') #no performance difference with or without this
# sct.shot() #code takes much more time (almost factor 10 higher compared to taking a large share of the screen)
main()