Is there a way to rename images based on their dominant color?

I have a folder full of PNG files made up of a single color… Here are a few of them to give an idea of what I’m trying to do:

Folder full of Colors

I want the name of the files to be their hex color codes. It would be really nice if there were some automated way of doing this.

I know there are ways to extract the color code from an image… theoretically it seems like it should be possible to rename each image with its resulting color code.

Just thought I’d see if someone here might have any ideas… I am on Windows

Anyone?

Thanks

Answer

OK so after some considerable searching I’ve now found a solution to this ‘dilemma’ of mine… Yay! Saved me so much work

The main key to making all of this possible at all is the ridiculously powerful (and free) software known as ImageMagick

*Note: On Windows at least, for full functionality ImageMagick also requires that Ghostscript be installed as well.

Starting filenames do not matter because they are handled using “*” as wildcard… Here is what I was starting with:

Dated Filenames - Useless

Using ImageMagick via the Command Prompt I was able to sample and extract the color of the image, then rename each image according to RGB values, output to decimal format changing the names from (for the file at top-right in the image found in the original question for example) from “2015-09-03_223717.png” to “rgb(102,130,154).png”.

This is done with the following command, run from within the folder:

magick *.png -set colorspace RGB -set filename:f "%[pixel:p]" %[filename:f].png

This makes a copy of each PNG file saved to the same folder, the originals can then be deleted. So that gets us one step closer. Some (ImageMagick users for instance) might find them more useful named this way… Here are the results:

Decimal RGB Codes as filenames

Now we need to change the filenames from decimal color codes to hex color codes (in this case minus the preceding “#” symbol)…

On Windows this requires the addition of the *nix tool known as “sed”, which I added using GOW (GNU On Windows), found here: https://github.com/bmatzelle/gow

So again from a Command Prompt within the folder enter:

for %I in ( *.png ) do ( magick %I -resize 1x1! txt: ^
   | sed -e "s/^#.*$//g;s/^.* #\(.*\)  .*$/ren %I \1\.png/g" >> hexnames.bat )

This will create (in the same folder) a BAT file with the necessary rename commands for each file. Then all we need to do is double-click the resulting BAT file to run it.

Done. Files have now been renamed (for example) from “rgb(102,130,154).png” to “66829A.png”. Final result:

Hex color codes as filenames

Attribution
Source : Link , Question Author : BANG , Answer Author : BANG

Leave a Comment