6
A Finder object implements an iterator of matches and allows to search for a visual
7
object in an image file that you provide (e.g. a screenshot taken and saved in a
8
file before). After setting up the finder object and doing a find operation, you can
9
iterate through the found matches if any.
13
* per definition, an iterator can be stepped through only once - it is empty
15
* it has to be destroyed using ``finder.destroy()``, especially when
16
used with ``for:`` or ``while:``
17
* when used in a ``with:`` construct, it is destroyed automatically
19
Compared with the region based find operation, no exception FindFailed is
20
raised in case nothing is found at all (use ``hasNext()`` to check). The finder object
21
can be compared to what you get with ``region.getLastMatches()`` when using :py:meth:`findAll() <Region.findAll>`.
23
**Note**: With this version, there is no chance, to get the number of matches in
24
advance. If you would iterate through to count, afterwards your finder would be
25
empty. So in this case, you have to save your matches somehow (one possible solution
28
The workflow always is, that you first do a find operation and afterwards go through the
29
matches found. After a complete iteration, the finder object is empty. So you
30
could start a new find operation again.
34
.. py:method:: Finder(path-to-imagefile)
36
Create a new finder object.
38
:param path-to-imagefile: filename to a source image to search within
40
.. py:method:: find(path-to-imagefile, [similarity])
42
Find a given image within a source image previously specified in the
43
constructor of the finder object.
45
:param path-to-imagefile: the target image to search for
46
:param similarity: the minimum similarity a match should have. If omitted,
49
.. py:method:: hasNext()
51
Check whether there are more matches available that satisfy the minimum
52
similarity requirement.
54
:return: *True* if more matches exist.
60
:return: a :py:class:`Match` object.
62
The returnd reference to a match object is no longer available in the finder
63
object afterwards. So if it is needed later on, it has to be saved to
67
Example 1: basic operations using a Finder
71
# create a Finder with your saved screenshot
72
f = Finder("stars.png")
73
img= "star.png" # the image you are searching
75
f.find(img) # find all matches
77
while f.hasNext(): # loop as long there is a first and more matches
78
print "found: ", f.next() # access the next match in the row
80
print f.hasNext() # is False, because f is empty now
81
f.destroy() # release the memory used by finder
83
Example 2: we want to know how many matches (based on the previous example).
87
# create a Finder with your saved screenshot
88
f = Finder("stars.png")
89
img= "star.png" # the image you are searching
91
f.find(img) # find all matches
92
mm = [] # an empty list
94
while f.hasNext(): # loop as long there is a first and more matches
95
mm.append(f.next()) # access next match and add to mm
97
print f.hasNext() # is False, because f is empty now
98
f.destroy() # release the memory used by finder
100
# now we have our matches saved in the list mm
101
print len(mm) # the number of matches
103
# we want to use our matches
b'\\ No newline at end of file'