Apr-05-2018, 07:55 PM
(Apr-05-2018, 07:07 PM)nilamo Wrote: It looks like your guess was right, most of the slowdown is in constantly taking a screenshot. Specifically, thepixel()
method is taking the most time, which is called bypixelMatchesColor()
. Since it looks like you often use the same pixel, and compare it against several colors, I'd suggest starting with writing a custompixelMatchesColor()
, that you pass a pixel's color to, instead of x/y coordinates. That way you're only taking a single screenshot each time and comparing it against a handful of things, instead of taking a handful of screenshots.
Something along the lines of:def pixelMatchesColor(color, matches, threshold=0): # should always be 3 or 4 (rbg, possibly with an alpha channel) for index in range(len(color)): if color[index]-threshold > matches[index]: # too high return False if color[index]+threshold < matches[index]: # too low return False return True def look(): first_cord, second_cord = position() pixel_color = pyautogui.pixel(first_cord, second_cord) find_look_left_1 = pixelMatchesColor(pixel_color, (95, 95, 0), tolerance=20) find_look_left_2 = pixelMatchesColor(pixel_color, (148, 148, 0), tolerance=30) find_look_right_1 = pixelMatchesColor(pixel_color, (0, 89, 0), tolerance=30) find_look_right_2 = pixelMatchesColor(pixel_color, (0, 45, 0), tolerance=30) find_look_hard_left_1 = pixelMatchesColor(pixel_color, (46, 0, 93), tolerance=20) find_look_hard_left_2 = pixelMatchesColor(pixel_color, (36, 0, 72), tolerance=20) find_look_hard_right_1 = pixelMatchesColor(pixel_color, (178, 89, 0), tolerance=20) find_look_hard_right_2 = pixelMatchesColor(pixel_color, (197, 98, 0), tolerance=20) # then your code progresses as before
You are a god! Can you reply or PM me how you went about fixing this and finding out the issue so I can learn from it?