Match
An object of class Match represents the result of a successful find operation. It has the rectangle dimension of the image, that was used to search. It knows the point of its upper left corner on an existing monitor, where it was found.
Since class Match extends class Region, all methods of
class Region can be used with a match object.
Creating a Match, Getting Attributes
Section titled “Creating a Match, Getting Attributes”A match object is created as the result of an explicit find operation. It can be
saved in a variable for later use with actions like Region.click.
It has the rectangle dimension of the image, that was used to search. It knows the point of its upper left corner on an existing monitor, where it was found. It knows the similarity it was found with and a click point to be used, if set by a pattern.
# m is a reference to a match object, if foundm = find("apple.png")print m # message area: Match[10,0 30x22] score=1.00, target=center
# m is a reference to a match object, if foundm = find(Pattern("apple.png").similar(0.5).targetOffset(100,0))print m # message area: Match[10,0 30x22] score=1.00, target=(105,11)For all other aspects, the features and attributes of class Region
apply.
getScore()
Section titled “getScore()”Get the similarity score the image or pattern was found. The value is between 0 and 1.
Returns: a decimal value between 0 (no match) and 1 (exact match).
getTarget()
Section titled “getTarget()”Get the location object that will be used as the click point.
Typically, when no offset was specified by Pattern.targetOffset,
the click point is the center of the matched region. If an offset was given,
the click point is the offset relative to the center.
Returns: the point defined by target offset (if set) or the center of the match.
getText()
Section titled “getText()”Returns the text that was stored on this match by a text-oriented find
operation (findWord, findLine, etc. — see Extracting Text from a Region).
For a match produced by an image find, the returned text is normally empty.
Returns: the recognized text as a String.
Iterating over Matches after findAll()
Section titled “Iterating over Matches after findAll()”A find operation Region.findAll returns an iterator object that can be
used to fetch all found matches as match objects one by one. A reference to the
iterator is stored in the respective
region and can be accessed using Region.getLastMatches.
Important to know:
- per definition, an iterator can be stepped through only once — it is empty afterwards
You can read more about the basics of operations with iterators from the description of
Finder class. To save contained matches for later use, you can
convert them to a list.
findAll("star.png") # find all matchesmm = list(getLastMatches())Example: using while: with default screen
findAll("star.png") # find all matchesmm = SCREEN.getLastMatches()while mm.hasNext(): # loop as long there is a first and more matches print "found: ", mm.next() # access the next match in the row
print mm.hasNext() # is False, because mm is empty nowprint mm.next() # is None, because mm is empty nowprint SCREEN.getLastMatches().hasNext() # is False also ;-)Example: using with: with default screen
with findAll("star.png") as mm: while mm.hasNext(): # loop as long there is a first and more matches print "found: ", mm.next() # access the next match# mm will be None afterwards (destroyed automatically)