This repository has been archived by the owner on Sep 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_process.py
51 lines (38 loc) · 1.59 KB
/
image_process.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from PIL import ImageGrab
from ctypes import windll
# enable DPI-aware behaviour for Pillow functions
user32 = windll.user32
user32.SetProcessDPIAware()
class ImageProcessor:
"""A collection of functions for turning images into Razer SDK keyboard-friendly representations"""
@staticmethod
def _get_resized_screen_pixels(columns=22, rows=6):
"""Grab a screenshot and resize to keyboard-friendly dimensions
:param columns: An integer, the number of columns on the keyboard
:param rows: An integer, the number of rows on the keyboard
:return: An Image object, the resized image
"""
img = ImageGrab.grab()
img = img.resize((columns, rows))
return img
@staticmethod
def get_keyboard_pixels():
"""Take a screenshot of the display, resize it and convert to a 6 x 22 list of BGR integers
:return: A 6 x 22 list of lists of BGR integers
"""
img = ImageProcessor._get_resized_screen_pixels()
pixel_list = []
for r in range(img.height):
row = []
for c in range(img.width):
p = ImageProcessor._rgb_to_bgr_int(img.getpixel((c, r)))
row.append(p)
pixel_list.append(row)
return pixel_list
@staticmethod
def _rgb_to_bgr_int(rgb):
"""Convert an rgb tuple into a corresponding BGR integer
:param rgb: A tuple in the format (R, G, B) where R, G, B are 0 - 255
:return: An integer, a BGR integer representation
"""
return rgb[0] * pow(2, 0) + rgb[1] * pow(2, 8) + rgb[2] * pow(2, 16)