Batch Rename Images According to Color?

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
WallBanger
Posts: 9
Joined: 2017-06-01T21:48:16-07:00
Authentication code: 1151

Batch Rename Images According to Color?

Post by WallBanger »

Hello. first post.

I have ImageMagick-7.0.5-Q16, I'm on Windows 10 x64...

I have a large 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:

Image

I would like to be able to batch rename the image files to be their hex color codes. So for example - the file at top left is currently named '2015-08-14_185213.png'... and the resulting filename would be simply B9B9D5.png. It would be really nice if there were some automated way of doing this.

I know there are ways to extract the hex color code from an image... (I've successfully tested output to a text file using: convert "FILENAME.png" -crop 1x1+44+44 "test.txt" )

Theoretically it seems like it should be possible to rename each image with its resulting color code. Is this doable at all?

Thanks in advance. ImageMagick is amazing!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Batch Rename Images According to Color?

Post by snibgo »

I don't think you can do this is a single "magick" command. But you can do it in a "magick" to get the colour in "txt:", followed by "ren".
snibgo's IM pages: im.snibgo.com
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Batch Rename Images According to Color?

Post by GeeMack »

WallBanger wrote: 2017-06-01T23:11:42-07:00I would like to be able to batch rename the image files to be their hex color codes. So for example - the file at top left is currently named '2015-08-14_185213.png'... and the resulting filename would be simply B9B9D5.png. It would be really nice if there were some automated way of doing this.
I don't know if there's a way to get the hex values straight from the image, but renaming (or actually converting) to filenames that represent their RGB values from 0 through 255 in decimal is easy. Assuming each PNG image you want to rename starts with "2016" and is a single color, something like this command will do that...

Code: Select all

magick 2016*.png ^
   -set filename:f "%[fx:int(p.r*255)]_%[fx:int(p.g*255)]_%[fx:int(p.b*255)]" %[filename:f].png
The result would be files named like "255_60_30.png", "28_112_182.png", "37_95_51.png", etc.

Edited to add:

Since Windows will let you use commas and parentheses in filenames you can also easily convert the images and save with output filenames formatted like "rgb(255,60,30).png", "rgb(28,112,182).png", "rgb(37,95,51).png", etc., using an IM command like this...

Code: Select all

magick 2016*.png -set colorspace RGB -set filename:f "%[pixel:p]" %[filename:f].png
Converting decimal to hex in a Windows BAT script can be done without external programs, but it's a bit clunky. There are *nix tools like "sed" and "awk" and "printf" available to run on a Windows CMD shell. The output of IM's "info:" or "txt:" can be used in a fairly simple BAT script using "printf", for example, to convert decimal to hex, then do a "ren" from the script as snibgo mentioned.
WallBanger
Posts: 9
Joined: 2017-06-01T21:48:16-07:00
Authentication code: 1151

Re: Batch Rename Images According to Color?

Post by WallBanger »

GeeMack wrote:
Windows will let you use commas and parentheses in filenames you can also easily convert the images and save with output filenames formatted like "rgb(255,60,30).png", "rgb(28,112,182).png", "rgb(37,95,51).png", etc., using an IM command like this...

Code: Select all

magick 2016*.png -set colorspace RGB -set filename:f "%[pixel:p]" %[filename:f].png
Nice! This works perfectly. So I now have a folder with the RGB values as the filenames as you've shown.

This gets me one step closer to what I'm trying to accomplish. Thanks a lot for this...
GeeMack wrote:
Converting decimal to hex in a Windows BAT script can be done without external programs, but it's a bit clunky. There are *nix tools like "sed" and "awk" and "printf" available to run on a Windows CMD shell. The output of IM's "info:" or "txt:" can be used in a fairly simple BAT script using "printf", for example, to convert decimal to hex, then do a "ren" from the script as snibgo mentioned.
Here you've lost me I'm afraid!

I've installed GOW (GNU On Windows) which does give me the "printf" capability - but as for how to use this to batch process all of my filenames, or how to transfer those results to the renaming of the correct files in my folder... this is way over my head I think.

I've spent hours now searching through posts here and there on decimal to hex conversions, etc and I'm more confused now than I was to begin with!

Perhaps it will be simpler for me to just manually sample the color, copy the hex code and rename all of the files? There are hundreds of files though so it is a daunting task. I had hoped there was some easy way to just grab only the hex values from the output of histogram:info or some such function, and send that to the filename.

It seems that these days outputting a hex color code would be more useful than decimal RGB values, so it seems odd that ImageMagick can do one but not the other... Is there something I'm missing here?

Anyhow you mentioned clunky decimal to hex BAT... I don't mind clunky as long as it works! Do you have a link or can you paste it here or something?

Thanks again, I really do appreciate your time.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Batch Rename Images According to Color?

Post by snibgo »

Code: Select all

for /f "usebackq skip=1 tokens=6 delims=,:()# " %A in (`%IM%convert toes.png -scale "1x1^!" txt:`) do echo %A
This takes the input file, toes.png, finds the average colour, and outputs the hex value. Instead of finding the average, you can crop to a single pixel.

Instead of "echo %A" you could have "ren toes.png %A.png".

You can stick that inside a for loop that processes all your files. Don't forget to double the % into %% if you use BAT scripts.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Batch Rename Images According to Color?

Post by fmw42 »

I have asked magick (the main IM developer) to create a new string escape %[hex:u.p{x,y}] like %[pixel:u.p{x,y}] but which provide output as hex values only. This should be available in the next release or possibly later today or tomorrow in the beta.
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Batch Rename Images According to Color?

Post by GeeMack »

WallBanger wrote: 2017-06-02T13:30:28-07:00I've installed GOW (GNU On Windows) which does give me the "printf" capability - but as for how to use this to batch process all of my filenames, or how to transfer those results to the renaming of the correct files in my folder... this is way over my head I think.
If your GNU utilities includes "sed" (or if you install the GNU sed for Windows) you can run an IM command in a "for" loop, the output piped through "sed" to create a BAT script. A command like this will do it...

Code: Select all

for %I in ( 2015*.png 2016*.png ) do ( magick %I -resize 1x1! txt: ^
   | sed -e "s/^#.*$//g;s/^.* #\(.*\)  .*$/ren %I \1\.png/g" >> fixnames.bat )
That would output a file named "fixnames.bat" which contains lines that look like "ren 2015-08-28_222155.png 679598.png" Then run the script to do the bulk rename.
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Batch Rename Images According to Color?

Post by GeeMack »

fmw42 wrote: 2017-06-02T14:50:47-07:00I have asked magick (the main IM developer) to create a new string escape %[hex:u.p{x,y}] like %[pixel:u.p{x,y}] but which provide output as hex values only. This should be available in the next release or possibly later today or tomorrow in the beta.
Very nice. This will make the OP's project into a quick streamlined operation. :)
WallBanger
Posts: 9
Joined: 2017-06-01T21:48:16-07:00
Authentication code: 1151

Re: Batch Rename Images According to Color?

Post by WallBanger »

snibgo wrote: 2017-06-02T14:22:12-07:00

Code: Select all

for /f "usebackq skip=1 tokens=6 delims=,:()# " %A in (`%IM%convert toes.png -scale "1x1^!" txt:`) do echo %A
This takes the input file, toes.png, finds the average colour, and outputs the hex value. Instead of finding the average, you can crop to a single pixel.

Instead of "echo %A" you could have "ren toes.png %A.png".

You can stick that inside a for loop that processes all your files. Don't forget to double the % into %% if you use BAT scripts.
Hey thanks for your time!

Obviously I don't have files named "toes.png" so I tried changing it to "*.png". I ran the cmd window from within the folder and I entered the following:

Code: Select all

for /f "usebackq skip=1 tokens=6 delims=,:()# " %A in (`%IM%convert *.png -scale "1x1^!" txt:`) do ren *.png %A.png
When I did that it returned this message:
'%IM%convert' is not recognized as an internal or external command, operable program or batch file.
I'm assuming the wildcard broke it somehow? Anyhow it seems that GeeMack has now solved this for me already below...

Thanks again for your efforts! Cheers
WallBanger
Posts: 9
Joined: 2017-06-01T21:48:16-07:00
Authentication code: 1151

Re: Batch Rename Images According to Color?

Post by WallBanger »

GeeMack wrote: 2017-06-02T15:11:43-07:00
WallBanger wrote: 2017-06-02T13:30:28-07:00I've installed GOW (GNU On Windows) which does give me the "printf" capability - but as for how to use this to batch process all of my filenames, or how to transfer those results to the renaming of the correct files in my folder... this is way over my head I think.
If your GNU utilities includes "sed" (or if you install the GNU sed for Windows) you can run an IM command in a "for" loop, the output piped through "sed" to create a BAT script. A command like this will do it...

Code: Select all

for %I in ( 2015*.png 2016*.png ) do ( magick %I -resize 1x1! txt: ^
   | sed -e "s/^#.*$//g;s/^.* #\(.*\)  .*$/ren %I \1\.png/g" >> fixnames.bat )
That would output a file named "fixnames.bat" which contains lines that look like "ren 2015-08-28_222155.png 679598.png" Then run the script to do the bulk rename.
Yes thankfully GOW does indeed include "sed". This is amazing, thank you so much!

That did exactly what I need. I ended up entering the following:

Code: Select all

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

And it worked absolutely flawlessly. From now on grabbing these color codes will be a snap...

My gratitude goes out to you... whoever, wherever you are you are definitely a cool guy!

Thanks again!

Cheers!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Batch Rename Images According to Color?

Post by fmw42 »

In IM 7.0.5.10 beta, this now works to directly return the color at a pixel in hex values. You need to use -depth if on Q16 to specify whether you want 8-bit hex values or 16-bit hex values.

Code: Select all

magick -size 100x100 xc:red -depth 8 -format "%[hex:u.p{0,0}]" info:
#FF0000

where {x,y} can be any pixel coordinate in the input image.

Or if the image is constant or you want the average hex color, you can do

Code: Select all

magick -size 100x100 xc:red -scale 1x1! -depth 8 -format "%[hex:u.p{0,0}]" info:
#FF0000
WallBanger
Posts: 9
Joined: 2017-06-01T21:48:16-07:00
Authentication code: 1151

Re: Batch Rename Images According to Color?

Post by WallBanger »

fmw42 wrote: 2017-06-02T14:50:47-07:00 I have asked magick (the main IM developer) to create a new string escape %[hex:u.p{x,y}] like %[pixel:u.p{x,y}] but which provide output as hex values only. This should be available in the next release or possibly later today or tomorrow in the beta.
That's so cool!

I am sure others will appreciate this as well considering the prevalence of the usage of hex color codes on the net these days...

Please also pass my gratitude and my kudos to magick as well. Clearly he is the man...

All of you here are so amazingly helpful it's almost surreal!

Just to be clear, if and when this functionality is implemented, the following code should work for these purposes:

Code: Select all

magick *.png -set colorspace RGB -set filename:f "%[hex:p]" %[filename:f].png
Is that correct?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Batch Rename Images According to Color?

Post by fmw42 »

See my note above that I seem to have posted at the same time as the OP
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Batch Rename Images According to Color?

Post by fmw42 »

If the image is a constant, it would be:

Code: Select all

magick *.png -depth 8 -set filename:f "%[hex:u.p{0,0}]" %[filename:f].png
If you want the color at x,y pixel coordinates, then

Code: Select all

magick *.png -depth 8 -set filename:f "%[hex:u.p{x,y}]" %[filename:f].png
You do not need the -set colorspace RGB, since you are getting hex values and not rgb(....) triplets.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Batch Rename Images According to Color?

Post by fmw42 »

Example using the IM 7.0.5.10 beta:

Code: Select all

magick logo: -depth 8 -set filename:f "%[hex:u.p{0,0}]" %[filename:f].png
produces an image named #FFFFFF.png
Post Reply