Batch rename with image dimensions

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?".
Post Reply
sokalsky
Posts: 2
Joined: 2012-05-29T14:04:09-07:00
Authentication code: 13

Batch rename with image dimensions

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Batch rename with image dimensions

Post 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
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Batch rename with image dimensions

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
sokalsky
Posts: 2
Joined: 2012-05-29T14:04:09-07:00
Authentication code: 13

Re: Batch rename with image dimensions

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Batch rename with image dimensions

Post 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.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Batch rename with image dimensions

Post 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?
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Batch rename with image dimensions

Post 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).
Post Reply