Skip to content

Commit

Permalink
Merge pull request #59 from spatiag/add-image-search-options
Browse files Browse the repository at this point in the history
Add missing image search options
  • Loading branch information
spyoungtech authored Aug 14, 2019
2 parents 8ccb428 + fda64f2 commit 6075d82
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
30 changes: 25 additions & 5 deletions ahk/screen.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
class ScreenMixin(ScriptEngine):
def image_search(self, image_path: str,
upper_bound: Tuple[int, int]=(0, 0), lower_bound: Tuple[int, int]=None,
coord_mode: str='Screen',
scale_height: int=None, scale_width: int=None) -> Union[Tuple[int, int], None]:
color_variation: int=None, coord_mode: str='Screen',
scale_height: int=None, scale_width: int=None,
transparent: str=None, icon: int=None) -> Union[Tuple[int, int], None]:
"""
`AutoHotkey ImageSearch reference`_
Expand All @@ -21,9 +22,18 @@ def image_search(self, image_path: str,
:param lower_bound: like ``upper_bound`` but for the lower-righthand corner of the search area e.g. (400, 800)
defaults to screen width and height (lower right-hand corner; ``%A_ScreenWidth%``, ``%A_ScreenHeight%``).
:param color_variation: Shades of variation (up or down) for the intensity of RGB for each pixel. Equivalent of
``*n`` option. Defaults to 0.
:param coord_mode: the Pixel CoordMode to use. Default is 'Screen'
:param scale_height: Scale height in pixels. Equivalent of ``*hn`` option
:param scale_width: Scale width in pixels. Equivalent of ``*wn`` option
:param transparent: Specific color in the image that will be ignored during the search. Pixels with the exact
color given will match any color. Can be used with color names e.g. (Black, Purple, Yellow) found
at https://https://www.autohotkey.com/docs/commands/Progress.htm#colors or hexadecimal values e.g.
(0xFFFFAA, 05FA15, 632511). Equivalent of ``*TransN`` option
:param icon: Number of the icon group to use. Equivalent of ``*Icon`` option
:return: coordinates of the upper-left pixel of where the image was found on the screen; ``None`` if the image
was not found
Expand All @@ -38,6 +48,17 @@ def image_search(self, image_path: str,
elif scale_width and not scale_height:
scale_height = -1

options = []
if icon:
options.append(f'Icon{icon}')
if color_variation:
options.append(color_variation)
if transparent:
options.append(f'Trans{transparent}')
if scale_width:
options.append(f'w{scale_width}')
options.append(f'h{scale_height}')

x1, y1 = upper_bound
if lower_bound:
x2, y2 = lower_bound
Expand All @@ -46,9 +67,8 @@ def image_search(self, image_path: str,
script = self.render_template('screen/image_search.ahk',
x1=x1, x2=x2, y1=y1, y2=y2,
coord_mode=coord_mode,
scale_width=scale_width,
scale_height=scale_height,
image_path=image_path)
image_path=image_path,
options=options)
resp = self.run_script(script)
try:
return ast.literal_eval(resp)
Expand Down
2 changes: 1 addition & 1 deletion ahk/templates/screen/image_search.ahk
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{% extends "base.ahk" %}
{% block body %}
CoordMode Pixel, {{ coord_mode }}
ImageSearch, xpos, ypos, {{ x1 }}, {{ y1 }}, {{ x2 }}, {{ y2 }}, {% if scale_width %}*w{{ scale_width}} *h{{ scale_height }} {% endif %}{{ image_path }}
ImageSearch, xpos, ypos, {{ x1 }}, {{ y1 }}, {{ x2 }}, {{ y2 }}, {% if options %}{% for option in options %}*{{ option }} {% endfor %}{% endif %}{{ image_path }}
s .= Format("({}, {})", xpos, ypos)
FileAppend, %s%, *
{% endblock body %}

0 comments on commit 6075d82

Please sign in to comment.