Python Forum
Looping issue, stops working
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Looping issue, stops working
#4
It looks like your guess was right, most of the slowdown is in constantly taking a screenshot. Specifically, the pixel() method is taking the most time, which is called by pixelMatchesColor(). Since it looks like you often use the same pixel, and compare it against several colors, I'd suggest starting with writing a custom pixelMatchesColor(), 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
Reply


Messages In This Thread
Looping issue, stops working - by JohnOsis - Apr-05-2018, 05:33 PM
RE: Looping issue, stops working - by nilamo - Apr-05-2018, 06:03 PM
RE: Looping issue, stops working - by JohnOsis - Apr-05-2018, 06:41 PM
RE: Looping issue, stops working - by nilamo - Apr-05-2018, 07:07 PM
RE: Looping issue, stops working - by JohnOsis - Apr-05-2018, 07:55 PM
RE: Looping issue, stops working - by nilamo - Apr-05-2018, 08:01 PM
RE: Looping issue, stops working - by JohnOsis - Apr-05-2018, 09:09 PM
RE: Looping issue, stops working - by nilamo - Apr-05-2018, 09:20 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  pip stops waiting for python walker 6 1,177 Nov-28-2023, 06:55 PM
Last Post: walker
  [SOLVED] [Linux] Script in cron stops after first run in loop Winfried 2 984 Nov-16-2022, 07:58 PM
Last Post: Winfried
  Last caracter of a string truncated issue when working from the end of the string Teknohead23 3 1,661 Oct-03-2021, 01:08 PM
Last Post: snippsat
  Know when the pyttsx3 engine stops talking UsualCoder 3 3,328 Aug-29-2021, 11:08 PM
Last Post: snippsat
  IDLE stops responding upon saving tompi1 2 1,988 Oct-01-2020, 05:44 PM
Last Post: Larz60+
  Infinite Looping Issue nsadams87xx 1 1,420 Jun-15-2020, 12:13 AM
Last Post: SheeppOSU
  Python timer script stops before should ozstar 3 2,273 May-04-2020, 12:55 AM
Last Post: ozstar
  Python stops without errors shahgourav 4 2,830 Feb-04-2020, 11:44 PM
Last Post: micseydel
  looping and indentation issue ameydiwanji 3 2,499 Jul-01-2019, 10:53 AM
Last Post: perfringo
  First for loop stops after first iteration Divanova94 10 9,092 May-01-2019, 04:27 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020