Skip to content

Commit 169ca07

Browse files
author
BoboTiG
committed
MSSLinux: few optimizations into get_pixels()
1 parent dd97dc3 commit 169ca07

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

mss.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
0.0.3 - remove PNG filters
2626
- remove 'ext' argument, using only PNG
2727
- do not overwrite existing image files
28+
- few optimizations into MSSLinux::get_pixels()
2829
2930
You can always get the latest version of this module at:
3031
@@ -528,6 +529,8 @@ def get_pixels(self, monitor):
528529
if image is None:
529530
raise ValueError('MSSLinux: XGetImage() failed.')
530531

532+
# TODO: how to optimize this part? pixels[offset:offset+3] is too long.
533+
'''
531534
pixels = [b'0'] * (3 * width * height)
532535
for x in range(width):
533536
for y in range(height):
@@ -537,6 +540,16 @@ def get_pixels(self, monitor):
537540
red = (pixel & 16711680) >> 16
538541
offset = (x + width * y) * 3
539542
pixels[offset:offset+3] = b(red), b(green), b(blue)
543+
#'''
544+
545+
# This code is a little bit better (19% faster)
546+
def pix(pixel):
547+
''' Apply shifts to a pixel to get the RGB values. '''
548+
return b((pixel & 16711680) >> 16) + b((pixel & 65280) >> 8) + b(pixel & 255)
549+
550+
get_pix = self.XGetPixel
551+
pixels = [pix(get_pix(image, x, y)) for y in range(height) for x in range(width)]
552+
540553
self.XFree(image)
541554
return b''.join(pixels)
542555

0 commit comments

Comments
 (0)