Skip to content

Instantly share code, notes, and snippets.

@BoboTiG
Last active March 19, 2018 15:46
Show Gist options
  • Save BoboTiG/d2466d524740776322abf6db9426f68c to your computer and use it in GitHub Desktop.
Save BoboTiG/d2466d524740776322abf6db9426f68c to your computer and use it in GitHub Desktop.
# coding: utf-8
from __future__ import print_function
import time
import numpy
from PIL import Image
import mss
def mss_rgb(im):
return im.rgb
def numpy_flip(im):
frame = numpy.array(im, dtype=numpy.uint8)
return numpy.flip(frame[:, :, :3], 2).tobytes()
def numpy_slice(im):
return numpy.array(im, dtype=numpy.uint8)[..., [2, 1, 0]].tobytes()
def pil_frombytes_rgb(im):
return Image.frombytes('RGB', im.size, im.rgb).tobytes()
def pil_frombytes(im):
return Image.frombytes('RGB', im.size, im.bgra, 'raw', 'BGRX').tobytes()
def benchmark():
with mss.mss() as sct:
im = sct.grab(sct.monitors[0])
for func in (pil_frombytes_rgb,
pil_frombytes,
mss_rgb,
numpy_flip,
numpy_slice):
count = 0
start = time.time()
while (time.time() - start) <= 1:
func(im)
im._ScreenShot__rgb = None
count += 1
print(func.__name__.ljust(17), count)
benchmark()
@BoboTiG
Copy link
Author

BoboTiG commented Mar 19, 2018

GNU/Linux:

$ python3 bench.py
pil_frombytes_rgb 51
pil_frombytes     139
mss_rgb           119
numpy_flip        31
numpy_slice       29
python2 bench.py
pil_frombytes_rgb 62
pil_frombytes     133
mss_rgb           121
numpy_flip        31
numpy_slice       28

@BoboTiG
Copy link
Author

BoboTiG commented Mar 19, 2018

macOS:

$ python3 bench.py
pil_frombytes_rgb 113
pil_frombytes     209
mss_rgb           174
numpy_flip        39
numpy_slice       36
$ python2 bench.py 
pil_frombytes_rgb 82
pil_frombytes     160
mss_rgb           132
numpy_flip        38
numpy_slice       34

@BoboTiG
Copy link
Author

BoboTiG commented Mar 19, 2018

Windows:

$ python2 bench.py
pil_frombytes_rgb 42
pil_frombytes     81
mss_rgb           66
numpy_flip        25
numpy_slice       22

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment