AHK: How to Extract Every Hex Code from an Image

Say for some reason, you really wanted to know the hex code for every color in an image. And you really wanted to use AutoHotkey. This is the program for you.

LowResHextractor

The Code

Hold up. It’s not entirely done yet. In the last few lines of code, there is a line starting with run, C:\ that needs to be manually adjusted to the document where the color codes should be stored. The rest is ready to go.

CoordMode, Pixel, Screen
CoordMode, Mouse, Screen

FileSelectFile, file,,, Select an Image:, Images (*.gif; *.jpg; *.bmp; *.png; *.tif)

Gui, +AlwaysOnTop -Caption
Gui, Add, Picture, , %file%
Gui, Show

WinGetPos, Xaxis, Yaxis, Width, Height, A
Xaxis := Xaxis + 10
Yaxis := Yaxis + 6
Width:= Width – 20
Height:= Height – 13

clipboard =

loop, %Height%
{
loop, %Width%
{
MouseMove, Xaxis+1, Yaxis+1
PixelGetColor, color, Xaxis, Yaxis
clipboard = %clipboard% %color%
Xaxis++
Sort, clipboard, D  U
continue
}
Xaxis:= Xaxis-Width
Yaxis++
}

Run, C:\Users\Desktop\Hex Extraction.txt
Sort, clipboard, D  U
SendInput {Ctrl down}v{Ctrl up} {return}

Gui Destroy
return

How It Works

Luckily, AutoHotkey has 3 commands that make this sort of thing ridiculously easy.

  1. FileSelectFile, Variable, Options, RootDirectory, Prompt, Filter (*.extension1; *.extension2; *.extension3)

FileSelectFile

2. PixelGetColor, Variable, X, Y

PixelGetColor

3. Sort, Variable, Options*
Options:
Dx tells the computer to separate entries by the character x
If a space is used, code will just sort entries by words.
U takes out duplicate entries.
variablesort

The first, FileSelectFile, allows the user to select a file for display in a GUI window. The second, PixelGetColor, does the real heavy lifting by actually getting the HEX value fram a pixel at a specified X and Y coordinate on your screen. A loop can be used to PixelGetColor for each pixel based on the height and width of the active window, appending each new pixel color to a set variable. The third, Sort, will alphabetize and remove unnecessary duplicates, so that the result will not have a gazillion entries of the same code.

Like so.

HextractorExplained

One Reply to “AHK: How to Extract Every Hex Code from an Image”

  1. Hey I was really happy when I found this code because i really really need something like this to work but sadly it doesnt seem to work for me. I’m not really a coder and could use a little help.
    The Code starts fine but when it comes to the end of the picture the arrow just moves across the border to the corner of my screen where it just stays. Is there an easy fix for that.
    I hope you still see this message an want to help.

    Greetings
    Niklas

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: