Enumerate Pixels Using For Loops - iseahound/ImagePut GitHub Wiki

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

Number of Parameters

The output depends on the number of parameters passed to the for-loop.

  • A 1-parameter for color in pic returns only the ARGB pixel
  • And the 3-parameter for x, y, color in pic returns the x, y coordinate as well.

Reference

  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, red, green, blue, alpha - The alpha represents the alpha channel.

Examples

; Use this code to load the pic variable.
pic := ImagePutBuffer("https://picsum.photos/2/3") ; 2 x 3 pixel image
pic.show(1)                                        ; or ImagePutWindow(pic)

The 1-parameter for-loop returns only the ARGB pixel.

for color in pic
    MsgBox color

The 3-parameter for-loop returns the x, y coordinate and the ARGB pixel.

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

The 6-parameter for-loop returns the x, y coordinate, the ARGB pixel, and the red, green, and blue components.

for x, y, color, red, green, blue in pic
    MsgBox x ", " y
    . "`n" c
    . "`n" r ", " g ", " b

The 7-parameter for-loop returns the x, y coordinate, the ARGB pixel, and the red, green, blue, and alpha channel components.

for x, y, color, red, green, blue, alpha in pic
    MsgBox x ", " y
    . "`n" c
    . "`n" r ", " g ", " b ", " a

The 2-parameter for-loop returns the index (starting from zero) and the ARGB color, as if the image were a 1-dimensional array.

for index, color in pic
    MsgBox index, color