2

I am learning autopy so I hope I can find help here. I am trying to find an image on my screen and move the cursor to it.

I have written the following code:

import autopy
# Take a screenshot of the entire desktop
autopy.bitmap.capture_screen().save('screen.png')

# define a function to search for the image in the screenshot
def find_image_example():
    aid = autopy.bitmap.Bitmap.open('aid.png') # searched image
    screen = autopy.bitmap.Bitmap.open('screen.png') # Desktop screenshot

    pos = screen.find_every_bitmap(aid,0.1) # find the image if any

    if pos:
        print('Found aid at: %s' % str(pos)) # print all found instances
        x = pos[0][0] # x pos
        y = pos[0][1] # y pos
        print(x) # print for debug
        print(y) # print for debug
        autopy.mouse.smooth_move(x,y) # move the cursor to the first image
    else:
        print('no images found!') # print if no images found
# execute the function
find_image_example()

after running the code, I get the following error message:

    Found aid at: [(405.0, 1000.0), (552.0, 1000.0), (699.0, 1000.0)]
405.0
1000.0
  File "C:/Users/Mortada/PycharmProjects/autopy_tut/main.py", line 22, in <module>
    find_image_example()
  File "C:/Users/Mortada/PycharmProjects/autopy_tut/main.py", line 18, in find_image_example
    autopy.mouse.smooth_move(x,y) # move the cursor to the first image
ValueError: Point out of bounds

I am not sure what is going wrong here :-/ I thought I git it right, lol!!

aid.png

captured screen

Mortada
  • 35
  • 4
  • I'm trying to replicate this code, but I'm not getting anything in my `pos` variable :(. Could you share the aid.png file? – LuRsT Sep 30 '18 at 18:10
  • UPDATE: I got it working nicely, so the code is correct. Without seeing more info like the screenshot your script takes, it's hard to debug. This is a cool lib, thanks to this question I learned something new :). – LuRsT Sep 30 '18 at 18:17
  • 1
    @LuRsT true it is a nice one indeed :-) I have added the screens – Mortada Sep 30 '18 at 18:29
  • Alright, I tested and I got the same error, but it's because my screen is smaller than yours, so I resized the images to half their size and the script worked. Are you using two monitors by any chance? – LuRsT Sep 30 '18 at 18:38
  • Are you running this on a mac? Autopy does not seem to work well with high dpi screens – Euphorbium Oct 14 '18 at 11:08

0 Answers0