forked from drov0/python-imagesearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.py
60 lines (43 loc) · 1.8 KB
/
example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from imagesearch import *
# Search for the github logo on the whole screen
# note that the search only works on your primary screen.
# This is intended to be used as examples to be copy pasted, do not run the whole file at once
pos = imagesearch("github.png")
if pos[0] != -1:
print("position : ", pos[0], pos[1])
pyautogui.moveTo(pos[0], pos[1])
else:
print("image not found")
# search for the github logo continuously :
pos = imagesearch_loop("github.png", 0.5)
print("image found ", pos[0], pos[1])
# search for the logo on the 0,0,800,600 region
# (a rectangle starting from the top left going 800 pixels to the right and down 600 pixels)
pos = imagesearcharea("github.png", 0, 0, 800, 600)
if pos[0] != -1:
print("position : ", pos[0], pos[1])
pyautogui.moveTo(pos[0], pos[1])
else:
print("image not found")
# the im parameter is usefull if you plan on looking for several different images without the need for recapturing the screen
# the screen capture being one of the most time consuming function it's a good way to optimize
# non -optimized way :
time1 = time.clock()
for i in range(10):
imagesearcharea("github.png", 0, 0, 800, 600)
imagesearcharea("panda.png", 0, 0, 800, 600)
print(str(time.clock() - time1) + " seconds (non optimized)")
# optimized way :
time1 = time.clock()
im = region_grabber((0, 0, 800, 600))
for i in range(10):
imagesearcharea("github.png", 0, 0, 800, 600, 0.8, im)
imagesearcharea("panda.png", 0, 0, 800, 600, 0.8, im)
print(str(time.clock() - time1) + " seconds (optimized)")
# sample output :
# 1.6233619831305721 seconds (non optimized)
# 0.4075934110084374 seconds (optimized)
# click image is to be used after having found the image
pos = imagesearch("github.png")
if pos[0] != -1:
click_image("github.png", pos, "right", 0.2, offset=5)