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?".
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 Windows, I think % need to be escape to %% in batch scripting
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-02T16:34:04-07:00 See my note above that I seem to have posted at the same time as the OP
Sorry, posted without seeing your reply
fmw42 wrote: 2017-06-02T16:38:35-07:00 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
produce an image named #FFFFFF.png
That's ridiculously easy now! I'm so glad I came here!

Just a couple more questions:

Where can I get the new beta?

How long does it usually take to go from beta to stable? Should I just wait a day or two?

You said in your last example that the output would be "#FFFFFF.png"... Is there an easy way to leave out the "#"?

These could be removed with a bulk rename easily enough I guess... just thought I'd ask since I'm on a roll here lol!
fmw42 wrote: 2017-06-02T16:46:26-07:00 In Windows, % need to be escape to %%
Hmmm... really? I entered the following code with single "%" previously and it worked without the need for double %%. Will the new code be different for some reason?

Code: Select all

magick *.png -set colorspace RGB -set filename:f "%[pixel:p]" %[filename:f].png
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 »

Betas are available only as source code that you need to compile. See http://magick.imagemagick.org/download/beta/

There is no particular time frame for when a beta gets officially release. It could be the next day or weeks. It depends upon how many and how significant the bug fixes are. Often release are made on the weekends, but not necessarily every weekend.

Right now there is no control for # or no #. Since this is in beta, it can be changed. I would say that perhaps it would be best to leave it off, since one can always do

magick image -depth 8 -format "#%[hex:u.p{0,0}]" info:

if one wants to add it. I have recommended the change.

% needs to be doubled, I believe only in batch scripting (.bat files). But I am not a Windows user. So I will defer to one of them to correct me. I shortly corrected my post about that above after your quote of it.
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-02T16:59:49-07:00 Right now there is no control for # or no #. Since this is in beta, it can be changed. I would say that perhaps it would be best to leave it off, since one can always do

magick image -depth 8 -format "#%[hex:u.p{0,0}]" info:

if one wants to add it. I have recommended the change.
I guess it helps to have friends in (virtual) high places eh?

Luckily for me what you've said makes good logical sense! It does seem as if it's easier to add a "#" character than it is to remove one...
fmw42 wrote: % needs to be doubled, I believe only in batch scripting (.bat files). But I am not a Windows user. So I will defer to one of them to correct me. I shortly corrected my post about that above after your quote of it.
Ah OK... Gotcha

A final and heartfelt thanks again to you sir, you are, as the old adage goes: a scholar and a gentleman, and you have saved me much time and many clicks!
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 »

Glad to have helped. I thought this would make a good addition and I hope the release comes out soon with the fix to remove the leading #
WallBanger
Posts: 9
Joined: 2017-06-01T21:48:16-07:00
Authentication code: 1151

Re: Batch Rename Images According to Color?

Post by WallBanger »

I wondered what I might do to show my appreciation... So I came back again to say:

If you guys fmw42 or magick in particular need any sort of graphic/design work done (perhaps some new icons or whatever), just let me know and I will do it for free
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 »

We are just happy to help you and to make the ImageMagick produce more useful and flexible. We will keep it in mind. Thanks for the offer.
WallBanger
Posts: 9
Joined: 2017-06-01T21:48:16-07:00
Authentication code: 1151

Re: Batch Rename Images According to Color?

Post by WallBanger »

Hi again...

Just popped back in to verify that the newest version of ImageMagick (7.0.6-0) now does indeed leave out the leading "#" symbol for hex color code output (awesome).

Here is the final script I ended up with (run from Windows Command Prompt):

Code: Select all

@ECHO OFF
  FOR %a in (*.png) DO (
    REN "*.png" "temp_*.png"
    magick mogrify -gravity Center -crop 88x88+0+0 +repage *.png
    magick *.png -depth 8 -set filename:f "%[hex:u.p{0,0}]" %[filename:f].png
    DEL "temp_*.png"
  )
  
This (for those who don't already know) will process all PNG files (script is for use with images which consist of only a single color) by:
  • Adding the prefix "temp_" to all filenames
  • Cropping them to 88x88px
  • Making copies with filenames according to their hex color codes, for example "679D42.png"
  • Deleting the originals
I wondered if there might be some way to do this all in one step using mogrify but I'm very happy with this method anyhow... So simple. Love it

Thanks again for all of your help... This makes this task so much easier now
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 »

If it works and you are happy, that's good.

But I notice that you loop through all of the PNG files (in the FOR loop).

Within that loop, you loop though all the files again (in the "mogrify".)

So you are repeating a lot of work. You never use "%a", so you can probably remove the FOR loop entirely.
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 »

This should work:

Code: Select all

cd folder_with_pngs
magick *.png -gravity center -crop 10x10+0+0 -depth 8 -set filename:f "%[hex:s.p{0,0}]" "%[filename:f].png"
Change your crop values as desired. The only issue is if you do not have enough memory to hold all your pngs at the same time.

Note the hex: uses s.p{0,0} so that it gets each image in the command sequence. u.p{0,0} will always take the first image in the sequence.
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-19T21:10:06-07:00
  • Adding the prefix "temp_" to all filenames
  • Cropping them to 88x88px
  • Making copies with filenames according to their hex color codes, for example "679D42.png"
  • Deleting the originals
I wondered if there might be some way to do this all in one step using mogrify but I'm very happy with this method anyhow.
All the IM operations can be done in a single command, but the renaming and deleting of the files will be done outside IM. A simple BAT script like this should get pretty close to what you're describing...

Code: Select all

@echo off

for %%I in ( *.png ) do ren "%%I" "temp_%%I"

magick *.png -depth 8 -resize 88x88! -set filename:f %%[hex:s.p{0,0}] %[filename:f].png

del "temp_*.png"
Post Reply