2019년 12월 17일 화요일

How to implement Autohotkey's imeagesearch function in Python

Hello everyone. For Work automation, Python!
So today we will learn how to implement Autohotkey's imeagesearch functionality in Python.

First, download imagesearch.py from drov0's Github. And put imeagesearch.py in py file's path to be created.
(https://github.com/drov0/python-imagesearch)

Click the [Clone or download] button, You will get imagesearch.py

Second, install following libraries. Run Python in the location of requirements.txt in the downloaded folder and proceed with pip3 install -r requirements.txt. The best thing to do is to install it through Anaconda and proceed with the installation of additional libraries.

Required libraries
- opencv-python
- numpy
- python3_xlib
- pyautogui

If do perfectly, preparation is completed!

Now put the image file you want to find in the path. Let's design a program that clicks on the Google logo.

Google logo


As shown below, capture only a part of Google logo and put in path as google.png file.

Capture the red box and make it a png file


Then write python file like below.

from imagesearch import *

img = './google_logo.png'
pos = imagesearch(img)
if pos[0] != -1:
    click_image(img, pos, 'left', 0.1)

Explanation

line 1 from imagesearch import *: imports imagesearch.py
line 3 img = './google_logo.png': Put the path of the image file you want to find in the variable img.
line 4 pos = imagesearch (img): Find the image file and insert the mouse coordinates in the variable pos.
line 5 if pos [0]! = -1: : If pos value is normal, excute.
line 6 click_image (img, pos, 'left', 0.1): Randomly clicks pos position within img size, having a 0.1 second mouse movement time.

Through your browser, open the Google logo window that looks like the image we have, and run that py file. If you recognize the image and click on the Google logo, it's a success!

Thank you.

Korean Language Version link is
(https://pyinweb.blogspot.com/2019/04/imagesearch.html)

댓글 없음:

댓글 쓰기