AutoIt PixelSearch Function

The PixelSearch function can be used to search for colors on the screen.

3765 views

Edited: 2017-03-24 17:00

The AutoIt PixelSearch Function is used to search a region of the screen for a pixel with a given color value, or a variation thereof.

PixelSearch either returns a two element array containing the coordinates of the pixel, or sets @error to 1 if color is not found.

PixelSearch is generally pretty fast, but if you think its to slow, narrowing the search area may help increase the performance.

Possible Values

pixelsLeft coordinate of rectangle.
pixelsTop coordinate of rectangle.
pixelsRight coordinate of rectangle.
pixelsBottom coordinate of rectangle.
colorColour value of pixel to find (in decimal or hex).
shade-variation [optional]A value between 0-255. This parameter allows to search for variations of the color provided. If provided, the number will search for a variation within both the Red, Geen, and Blue values of the color.
step [optional]A value larger then 1 will skip pixels, instead of searching each pixel. Effects the speed of the search.
hwnd [optional]Window handle.

PixelSearch Examples

Looks for a pure Red Pixel, in the region 0,0 (Start) - 100,200 (end).

$coord = PixelSearch(0, 0, 100, 200, 0xFF0000)
If Not @error Then
    MsgBox(0, "X and Y are:", $coord[0] & "," & $coord[1])
EndIf

If you want to search for variations, then simply add the next parameter.

$coord = PixelSearch(0, 0, 100, 200, 0xFF0000, 5)
If Not @error Then
    MsgBox(0, "X and Y are:", $coord[0] & "," & $coord[1])
EndIf

To click the returned coordinates, use something like the below.

$coord = PixelSearch(0, 0, 100, 200, 0xFF0000, 5)
If Not @error Then
    MouseClick("primary", $coord[0], $coord[1], 1, 0)
EndIf

If you want to click somewhere near where the color was found, say above or below, than simply add the offset to the coordinates. Just be careful that you don't attempt to click outside the screen. The below will click 15 pixels above the located color:

$coord = PixelSearch(0, 15, 100, 200, 0xFF0000)
If Not @error Then
  MouseClick("primary", $coord[0], $coord[1]-15, 1)
EndIf

Links

  1. PixelSearch - autoitscript.com

Tell us what you think:

  1. How to declare and work with variables in AutoIt, as well as some background information.
  2. Everything you need to know about working with minimized windows.
  3. How to set the request headers when performing HTTP requests.
  4. This Tutorial will focus on post requests in AutoIt, using the Winhttprequest.5.1 object.
  5. How to use while, for, and do until loops, and how to loop through arrays and object in AutoIt.

More in: AutoIt Tutorials