Skip to content

Enumerate Pixels Using For Loops

Edison Hua edited this page Jan 10, 2023 · 13 revisions

Only on AutoHotkey version 2

Simple Example

Enumerates all the pixels in the image.

pic := ImagePutBuffer("https://picsum.photos/2/3") ; 2 x 3 pixel image
pic.show(1)                                        ; or ImagePutWindow(pic)
for color in pic                                   ; Enumerate a single value
    MsgBox color                                   ; Get color

Depends on the number of parameters in the for-loop, such as the 1 parameter for color in pic or 3 parameter for x, y, color in pic.

  1. color - The color is an ARGB unsigned 32-bit integer with 256 values for each color channel.
  2. index, color - The index is a sequential ordering of pixels starting from zero.
  3. x, y, color - The [x, y] coordinates denote the current x, y position of the color.
  4. Invalid number of parameters.
  5. Invalid number of parameters.
  6. x, y, color, red, green, blue - The red, green, blue represents the individual color channel and ranges from 0-255.
  7. x, y, color, alpha, red, green, blue - The alpha represents the alpha channel.

The above example Returns the x, y coordinate and the color.

for x, y, c in pic
   pic[x, y] := 0xFF0000 ; Sets the x, y to red.

Here you can get the red, green, and blue components.

for x, y, c, r, g, b in pic
   MsgBox x ", " y
   . "`n" c
   . "`n" r ", " g ", " b

Here you can get the red, green, blue, and alpha components.

for x, y, c, r, g, b, a in pic
   MsgBox x ", " y
   . "`n" c
   . "`n" r ", " g ", " b ", " a

If for some reason you just want the image as a 1-dimensional array

for i, c in pic
   Msgbox i