Commit 1c46685d authored by Taddeüs Kroes's avatar Taddeüs Kroes

Get rid of PIL, do HSV conversion in pure Python instead

parent efdf4ac5
...@@ -5,7 +5,6 @@ sys.path.insert(0, join(dirname(__file__), 'python-xlib')) ...@@ -5,7 +5,6 @@ sys.path.insert(0, join(dirname(__file__), 'python-xlib'))
import time import time
from random import randint from random import randint
from Xlib import display, X, XK, ext from Xlib import display, X, XK, ext
from PIL import Image
WINDOW_WIDTH = 1600 WINDOW_WIDTH = 1600
...@@ -49,11 +48,9 @@ def focus_window(window): ...@@ -49,11 +48,9 @@ def focus_window(window):
def screenshot_board(window): def screenshot_board(window):
raw = window.get_image(BOARD_X, BOARD_Y, BOARD_WIDTH, BOARD_HEIGHT, im = window.get_image(BOARD_X, BOARD_Y, BOARD_WIDTH, BOARD_HEIGHT,
X.ZPixmap, 0xffffffff) X.ZPixmap, 0xffffffff)
dim = BOARD_WIDTH, BOARD_HEIGHT return BoardImage(im.data)
im = Image.frombytes('RGB', dim, raw.data, 'raw', 'BGRX')
return im.convert('HSV')
def press_keys(window, keys): def press_keys(window, keys):
...@@ -71,8 +68,39 @@ def press_keys(window, keys): ...@@ -71,8 +68,39 @@ def press_keys(window, keys):
time.sleep(KEY_DELAY / 1000) time.sleep(KEY_DELAY / 1000)
def rgb2hsv(r, g, b):
r /= 255
g /= 255
b /= 255
cmax = max(r, g, b)
cmin = min(r, g, b)
diff = cmax - cmin
if cmax == cmin:
h = 0
elif cmax == r:
h = (60 * ((g - b) / diff) + 360) % 360
elif cmax == g:
h = (60 * ((b - r) / diff) + 120) % 360
elif cmax == b:
h = (60 * ((r - g) / diff) + 240) % 360
h = int(h * 256 / 360)
s = 0 if cmax == 0 else int(diff * 256 / cmax)
v = int(cmax * 256)
return h, s, v
class BoardImage:
def __init__(self, data):
self.data = data
def getpixel(self, xy):
x, y = xy
i = (y * BOARD_WIDTH + x) * 4
b, g, r = self.data[i:i + 3]
return rgb2hsv(r, g, b)
if __name__ == '__main__': if __name__ == '__main__':
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment