Why PyAutoGui LocateOnScreen() only Returns None

前端 未结 11 1898
悲&欢浪女
悲&欢浪女 2020-12-15 10:39

Here\'s the code that I\'m trying to run:

import pyautogui
r=pyautogui.locateOnScreen(\'C:\\Users\\David\\Desktop\\index.png\',grayscale=False)
print r


        
相关标签:
11条回答
  • 2020-12-15 11:15

    I had the similar problem.

    My fault was I had saved the compare picture as jpg first and then as png in MS paint.

    Be sure to save the compare picture as png format. After this the Locate function worked for me.

    0 讨论(0)
  • 2020-12-15 11:21

    I found a way to fix my problem. Only search for an image as small as possible. A picture that is only 1 pixel is found after 3 seconds. And when i try to search for an image over 500x500 then it won't find anything.

    0 讨论(0)
  • 2020-12-15 11:22

    I was encountering the same problem, what I did is

    import pyautogui 
    r= None 
    while r is None:
        r=pyautogui.locateOnScreen('C:\Users\David\Desktop\index.png',grayscale=False)
    print r
    

    I think its just because that it takes time to locate image. If you found a better solution share with me :)

    0 讨论(0)
  • 2020-12-15 11:22

    I got this working by using the following:

    r = None
    while r is None:
        r = pyautogui.locateOnScreen('rbin.PNG', grayscale = True)
    print icon_to_click + ' now loaded'
    

    The key is to make grayscale = True.

    0 讨论(0)
  • 2020-12-15 11:23

    The official documentation says;

    The Locate Functions
    NOTE: As of version 0.9.41, if the locate functions can’t find the provided image, 
    they’ll raise ImageNotFoundException instead of returning None.
    

    So you can decide whether an exception was raise or not. Also you should try for a finite number of times not a While True loop.

    retry_counter = 0
    while retry_counter < 5:
        try:
            result = pyautogui.locateOnScreen(IMAGE_PATH_TO_FIND)
            if result:
                time.sleep(1)
                retry_counter = 10  # to break the loop
        except:
            time.sleep(1)  # retry after some time, i.e. 1 sec
            retry_counter += 1
    
    0 讨论(0)
  • 2020-12-15 11:28

    I had that problem but then i crop the photo into specific part then it was locating and yes it takes time.

    or this can also work.

    b = pyautogui.center('calc7key.png')
    
    0 讨论(0)
提交回复
热议问题