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?".
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: 26383
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: 8884
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: 26383
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: 8884
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: 26383
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).

NITO
Posts: 33
Joined: 2019-12-31T17:28:13-07:00
Authentication code: 1152
Location: Brasil - São Paulo

Re: Batch rename with image dimensions

Post by NITO »

Hi, I'm using ImageMagick-7.0.9-12-Q16-x64-dll on Windows 10, using CMD, and I can't rename using resolution. can you help me?

snibgo
Posts: 13034
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Batch rename with image dimensions

Post by snibgo »

Your question is vague. Please show examples of inputs, and what you want the output filenames to be.
snibgo's IM pages: im.snibgo.com

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

Re: Batch rename with image dimensions

Post by fmw42 »

Please do not tack a new question onto an old one. Start a new question and explain it fully and show the code you have tried.

NITO
Posts: 33
Joined: 2019-12-31T17:28:13-07:00
Authentication code: 1152
Location: Brasil - São Paulo

Re: Batch rename with image dimensions

Post by NITO »

snibgo wrote:
2019-12-31T19:00:58-07:00
Your question is vague. Please show examples of inputs, and what you want the output filenames to be.
I am using this sequence no .bat

magick convert -density 300 * _labels.pdf -set filename:mysize "%%wx%%h" %%[filename:mysize].png

There are several PDF files in the folder, I need them to be converted to png and renamed with the resolution all the pages, but it can only do one of each resolution, but many with the same resolution.
It just renames a file by resolution eg 600x400.png.

But I need to rename it 600x400_01.png, 600x400_02.png, 600x400_03.png, and so on.

NITO
Posts: 33
Joined: 2019-12-31T17:28:13-07:00
Authentication code: 1152
Location: Brasil - São Paulo

Re: Batch rename with image dimensions

Post by NITO »

fmw42 wrote:
2019-12-31T23:29:31-07:00
Please do not tack a new question onto an old one. Start a new question and explain it fully and show the code you have tried.
I asked here, because it is the same subject, what changes is the version of Magick and Windows, and did not find another topic on this subject.

User avatar
fmw42
Posts: 26383
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 just magick and not magick convert. If that fails, then your install may be flawed. Why are you doubling your %. Is this in a .bat file? You cannot use wild cards, *, in the input and get multiple outputs for each. The following works for me for one 4-page PDF file in IM 7.0.9.13 Mac OSX.

Code: Select all

magick image.pdf -set filename:mysize "%wx%h" "%[filename:mysize]_%d.png"
It does not seem to work without the _%d as:

Code: Select all

magick image.pdf -set filename:mysize "%wx%h" "%[filename:mysize].png"
Next time post a new question. They are not the same things, since you are using a PDF as input, which has multiple pages. Also the original question was 8-years ago and IM 7 is different from IM 6.

NITO
Posts: 33
Joined: 2019-12-31T17:28:13-07:00
Authentication code: 1152
Location: Brasil - São Paulo

Re: Batch rename with image dimensions

Post by NITO »

I am using joker because in the folder has several PDF, and I need to convert all.
I put two% because it is on .bat.
when I put% d it just renames the file to 600x400_d.png and for that, if I put only one% it renames and converts all but without resolution.
The pdf has more than 4 pages, with three resolutions, it only converts one of each resolution.

with two %% - magick -density 300 *_labels.pdf -set filename:mysize "%%wx%%h" "%%[filename:mysize]_%d.png"

Image

with only one% - magick -density 300 *_labels.pdf -set filename:mysize "%wx%h" "%[filename:mysize]_%d.png"

Image

the folder with the pdfs

Image

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

Re: Batch rename with image dimensions

Post by fmw42 »

You cannot do wild cards with * to process all PDFs in a folder. You need to do them one at a time. Or write a script loop over each PDF in the folder and process it that way. If that does not work, then post one of your input PDF files.

Locked