-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathfull_examples.py
73 lines (54 loc) · 2.36 KB
/
full_examples.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
61
62
63
64
65
66
67
68
69
70
71
72
73
from python_imagesearch.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 until found :
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 useful 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)
# when you have various images to find on the screen, you can
# use this function, it iterates through the files in the path
# you provide and outputs an dictionary where the key is the path
# to the file and the value is the position array.
#
# I create this to be used with the same image with different sizes,
# so no matter the size it appears on the screen, i can find it.
#
# In this example it iterates through the main folder of the project
# and find the panda.png and github.png
print(str(imagesearch_from_folder('./', 0.8)))