ImageSearch

画面内の領域を検索して画像を探します。

ImageSearch &OutputVarX, &OutputVarY, X1, Y1, X2, Y2, ImageFile

パラメータ

&OutputVarX, &OutputVarY

型:VarRef

画面上で画像が見つかった場所の左上ピクセルのX、Y座標を格納する出力変数への参照(一致しない場合は空白とする)。Coordinates are relative to the active window's client area unless CoordMode was used to change that.

X1, Y1

型:整数

検索する矩形の左上隅のX、Y座標。Coordinates are relative to the active window's client area unless CoordMode was used to change that.

X2, Y2

型:整数

検索する矩形の右下隅のX、Y座標。Coordinates are relative to the active window's client area unless CoordMode was used to change that.

ImageFile

型:文字列

画像のファイル名。絶対パスが指定されていない場合は、A_WorkingDirにあると仮定されます。対応する画像形式は、ANI、BMP、CUR、EMF、Exif、GIF、ICO、JPG、PNG、TIF、WMF(BMP画像は16ビット以上)です。その他、アイコンのソースとして、以下のような種類のファイルがあります:EXE、DLL、CPL、SCRなど、アイコンリソースを含むタイプ。

Options: Zero or more of the following options may be also be present immediately before the name of the file. 各オプションと次のオプションは、1つのスペースまたはタブで区切ってください。事例:"*2 *w100 *h-1 C:\Main Logo.bmp".

*IconN: To use an icon group other than the first one in the file, specify *Icon followed immediately by the number of the group. For example, *Icon2 would load the default icon from the second icon group.

*n (variation):Specify for n a number between 0 and 255 (inclusive) to indicate the allowed number of shades of variation in either direction for the intensity of the red, green, and blue components of each pixel's color. For example, if *2 is specified and the color of a pixel is 0x444444, any color from 0x424242 to 0x464646 will be considered a match. This parameter is helpful if the coloring of the image varies slightly or if ImageFile uses a format such as GIF or JPG that does not accurately represent an image on the screen. 255色のバリエーションを指定した場合、すべての色が一致します。デフォルトは0階調です。

*TransN:このオプションは、画面上のどの色とも一致するような色を画像内で1つ指定することで、一致する色を見つけやすくするものです。透明な部分があるPNG、GIF、TIFファイルを探すのに最もよく使われます(ただし、アイコンは自動的に透明化されるため、このオプションは必要ありません)。For GIF files, *TransWhite might be most likely to work. For PNG and TIF files, *TransBlack might be best. Otherwise, specify for N some other color name or RGB value (see the color chart for guidance, or use PixelGetColor in its RGB mode). 例:*TransBlack, *TransFFFFAA, *Trans0xFFFFAA.

*wn and *hn:画像を拡大縮小する幅と高さ(この幅と高さは、マルチアイコン.ICOファイルからどのアイコンを読み込むかも決定)。If both these options are omitted, icons loaded from ICO, DLL, or EXE files are scaled to the system's default small-icon size, which is usually 16 by 16 (you can force the actual/internal size to be used by specifying *w0 *h0). アイコンでない画像は、実際のサイズで読み込まれます。画像のアスペクト比を保ったまま縮小・拡大する場合は、一方の寸法に-1、他方の寸法に正の数を指定します。For example, specifying *w200 *h-1 would make the image 200 pixels wide and cause its height to be set automatically.

ファイル名の代わりにビットマップやアイコンハンドルを使うこともできる。例えば、"HBITMAP:*"ハンドルです。

戻り値

型:整数(ブーリアン)

この関数は、指定された領域で画像が見つかった場合は 1(真)を、見つからなかった場合は 0(偽)を返します。

エラー処理

無効なパラメータが検出された場合、または画像を読み込むことができなかった場合、ValueErrorがスローされます。

内部関数呼び出しに失敗した場合、OSErrorが投げられる。

備考

ImageSearchは、テキストがない、またはテキストを容易に検索できない画面上のグラフィカルオブジェクトを検出するために使用されます。例えば、絵柄のボタンやアイコン、Webページのリンク、ゲームオブジェクトの位置などを発見するのに利用できます。このようなオブジェクトは、位置が決まれば、Clickでクリックすることが可能です。

画像全体ではなく、画像から小さく切り取ったものを検索するのも有効な手段です。これにより、画像全体が変化しても、その中のある部分は常に同じであるような場合の信頼性を向上させることができます。クリッピングを抽出する方法の1つです:

  1. アクティブウィンドウに画像が表示されている状態で、 Alt+PrtScを押します。クリップボードにスクリーンショットを配置します。
  2. ペイントなどの画像処理ソフトを起動します。
  3. クリップボードの内容(つまり、スクリーンショット)を貼り付けます。
  4. ばらつきがなく、画像に特徴がある領域を選択します。
  5. その領域をコピーして、新しい画像ドキュメントに貼り付けます。
  6. ImageSearchで使用するために小さなファイルとして保存してください。

一致するためには、画面上の画像は、ImageFileパラメータとそのオプションで読み込まれた画像と同じサイズでなければなりません。

検索する領域は可視でなければなりません。言い換えれば、ウィンドウが他のウィンドウの後ろに隠れている領域を検索することは不可能です。一方、マウスカーソルの下に部分的にある画像は、通常、検出することができます。ただし、ゲームカーソルは例外で、ほとんどの場合、その下にある画像が邪魔になります。

検索は領域の最上段から始まり、下へ下へと移動していくので、複数のマッチングがある場合は、一番上に近いものが検索されることになります。

透明な色を含むアイコンは、自動的にその色が画面上のどの色とも一致するようになります。したがって、アイコンの後ろにあるものの色は関係ありません。

ImageSearchは、8ビットカラー画面(256色)以上に対応しています。

ディスプレイアダプターの色深度により、検索動作が異なる場合があります(特にGIFファイル、JPGファイル)。したがって、複数の色深度でスクリプトを実行する場合は、各色深度の設定でテストするのがベストです。shades-of-variationオプション(*n)を使用することで、複数の色深度での動作の一貫性を持たせることができます。

画面上の画像が半透明な場合、ImageSearchはおそらく検索に失敗します。これを回避するには、shades-of-variationオプション(*n)を試すか、WinSetTransparent("Off")でウィンドウを一時的に不透明にする必要があります。

PixelSearch, PixelGetColor, CoordMode, MouseGetPos

アクティブウィンドウの画像領域を検索し、画像が見つかった場所の左上ピクセルのX座標とY座標をFoundXFoundYに格納します。

ImageSearch &FoundX, &FoundY, 40, 40, 300, 300, "C:¥My Imagestest.bmp"

高度なエラー処理を含め、画面の領域を画像検索し、画像が見つかった場所の左上ピクセルのXおよびY座標をFoundXおよびFoundYに格納します。

CoordMode "Pixel"  ; Interprets the coordinates below as relative to the screen rather than the active window's client area.
try
{
    if ImageSearch(&FoundX, &FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, "*Icon3 " A_ProgramFiles "\SomeApp\SomeApp.exe")
        MsgBox "The icon was found at " FoundX "x" FoundY
    else
        MsgBox "Icon could not be found on the screen."
}
catch as exc
    MsgBox "Could not conduct the search due to the following error:`n" exc.Message