Как мне оптимизировать цикл, чтобы он работал намного быстрее?
import keyboard
import pyscreenshot as ImageGrab
from Xlib import display
def move_left():
keyboard.send('right')
def move_right():
keyboard.send('left')
def exist_branch(x, y):
box = (x, y - 1 * 85, x + 1, y)
im = ImageGrab.grab(box)
rgb_im = im.convert('RGB')
x, y = im.size
result = []
for i in range(0, 1):
r, g, b = rgb_im.getpixel((0, y - 1 - i * 85))
summa = r + g + b
if summa == 301 or summa == 289 or summa == 244 or summa == 210 or summa == 248 or summa == 217:
result.append(False)
else:
result.append(True)
return result
def get_mouse():
while True:
data = display.Display().screen().root.query_pointer()._data
x = data["root_x"]
y = data["root_y"]
print
'%s,%s - %s' % (str(x), str(y), exist_branch(x, y))
def main():
start_x = 1033
start_y = 715
while True:
branches = exist_branch(start_x, start_y)
branches1 = exist_branch(start_x, 800)
for elem in branches:
if elem:
for elem in branches1:
if elem:
move_left()
else:
move_right()
else:
move_right()
try:
# get_mouse()
main()
except:
print
'Exit..'