Search code examples
batch-processingphotoshopphotoshop-script

Batch extract Hex colour from images to file


I have around 10k images that I need to get the Hex colour from for each one. I can obviously do this manually with PS or other tools but I'm looking for a solution that would ideally:

  1. Run against a folder full of JPG images.
  2. Extract the Hex from dead center of the image.
  3. Output the result to a text file, ideally a CSV, containing the file name and the resulting Hex code on each row.

Can anyone suggest something that will save my sanity please? Cheers!


Solution

  • I would suggest ImageMagick which is installed on most Linux distros and is available for OSX (via homebrew) and Windows.

    So, just at the command-line, in a directory full of JPG images, you could run this:

    convert *.jpg -gravity center -crop 1x1+0+0 -format "%f,%[fx:int(mean.r*255)],%[fx:int(mean.g*255)],%[fx:int(mean.b*255)]\n" info:
    

    Sample Output

    a.png,127,0,128
    b.jpg,127,0,129
    b.png,255,0,0
    

    Notes:

    If you have more files in a directory than your shell can glob, you may be better of letting ImageMagick do the globbing internally, rather than using the shell, with:

    convert '*.jpg' ...
    

    If your files are large, you may better off doing them one at a time in a loop rather than loading them all into memory:

    for f in *.jpg; do convert "$f" ....... ; done