Page 1 of 1

Batch rename with image dimensions

Posted: 2012-05-29T14:16:38-07:00
by sokalsky
Hi All,

I am a newcomer to both ImageMagick and scripts so please excuse my ignorance. I am using IM 6.7.7-0 2012-05-17 Q16 and running a script on Windows 7 using Cygwin v4.1.10(4).

I am trying to batch rename thousands of small images with their resolution (width & height) as a prefix, followed by the existing filename. So I would like "PICFILE01.PNG" (with a resolution of 24x24) to become "24x24 PICFILE01.PNG".

Here's what I'm using so far (which basically just renames each file with it's resolution). I'm having trouble appending the original filename to the end of the file.

for filename in *.png ;
do
newname="`identify -format %w%h $filename`"
echo mv "$filename" "${newname}.png"; <-- Need help here adding in the original filename.
done

Any help is much appreciated!

Thanks

Re: Batch rename with image dimensions

Posted: 2012-05-29T15:37:40-07:00
by fmw42
try this as an example. then you can put it into your script

# create a png image from the IM internal rose: image
convert rose: rose.png

# rename the rose.png image
inname=`convert rose.png -format "%t" info:`
size=`convert rose.png -format "%wx%h" info:`
mv ${inname}.png "${size} ${inname}.png"

Note the quotes in the output name since you want a space after the size. I would recommend you put an underscore rather than a space as spaces can be hard to deal with. Then you don't need the quotes.


see
http://www.imagemagick.org/script/escape.php

Re: Batch rename with image dimensions

Posted: 2012-05-30T01:10:33-07:00
by anthony
Im can calculate the name, but for security you need to do so in a particular way.

See Filename Percent Escapes for examples...
http://www.imagemagick.org/Usage/files/#save_escapes

The very first example is an example of what you are wanting.

Re: Batch rename with image dimensions

Posted: 2012-05-30T14:02:40-07:00
by sokalsky
Thanks so much! Here's what i ended up with:

for filename in *.png*;
do
inname=`convert $filename -format "%t" info:`
size=`convert $filename -format "%wx%h" info:`
mv $filename "${size}_${inname}.png";
done

It works great (I took the advice of getting rid of the space and replacing with an underscore), but I am having a weird effect: it looks like an extra character is being appended after the variables in the output filenames:

24x24_pcfile.png

Those little box characters are added to all output filenames. Any idea why this might be occurring?

Thanks again for the help - saved me tons of time.

Re: Batch rename with image dimensions

Posted: 2012-05-30T15:26:44-07:00
by fmw42
I don't seem to have that problem on my Mac. Works just fine. Could be a line ending issue? Did you copy and paste? That might introduce some "gremlins". Try typing from scratch and test on one single image to see if that works.

Re: Batch rename with image dimensions

Posted: 2012-05-30T20:04:41-07:00
by anthony
If it is the latest IMv6, then I may have introduced a bug in escape handling, but I can't seem to reproduce it either.

As such exactly what version of IM are you using?

Re: Batch rename with image dimensions

Posted: 2012-05-30T20:14:32-07:00
by fmw42
anthony wrote:If it is the latest IMv6, then I may have introduced a bug in escape handling, but I can't seem to reproduce it either.

As such exactly what version of IM are you using?

Anthony,

In his first post, he says:

IM 6.7.7-0 2012-05-17 Q16 and running a script on Windows 7 using Cygwin v4.1.10(4).