Add dimention into filename

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
AlexNovak
Posts: 3
Joined: 2016-04-18T05:36:43-07:00
Authentication code: 1151

Add dimention into filename

Post by AlexNovak »

How can I add image dimention into it's filename? Truly speaking, I can't even extract it with identify :(

This code:

Code: Select all

@echo off
identify -format "%f, %w, %h" sprite.jpg
pause
Gives me this:

Code: Select all

w, hPress any key to continue . . .

PS:
I'm using ImageMagick 6.9.3-7 Q16 x64 and Windows 7 x64
Last edited by AlexNovak on 2016-04-18T08:48:12-07:00, edited 1 time in total.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Add dimention into filename

Post by snibgo »

You should always tell us the IM version you are using, and the platform (Windows,Unix, whatever).

From your "@echo off", I assume this is a Windows BAT script. When you use percent % in BAT scripts, to prevent Windows interpreting them as script parameters, you need to double them:

Code: Select all

identify -format "%%f, %%w, %%h" sprite.jpg
snibgo's IM pages: im.snibgo.com
AlexNovak
Posts: 3
Joined: 2016-04-18T05:36:43-07:00
Authentication code: 1151

Re: Add dimention into filename

Post by AlexNovak »

Thank you snibgo, it works! But how can I "convert" that info into a value of some var? set VAR=... interpritate that whole command as a string.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Add dimention into filename

Post by snibgo »

Something like this:

Code: Select all

for /F "usebackq" %%L in (`identify ^
  -format "FILE=%%f\nWIDTH=%%w\nHEIGHT=%%h" ^
  sprite.jpg`) do set %%L
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: Add dimention into filename

Post by GeeMack »

AlexNovak wrote:How can I add image dimention into it's filename?
This line in a Windows BAT file would take every JPG in the directory and rename it to its original file name, followed by an underscore, followed by WxH, and ending with ".jpg"...

Code: Select all

convert *.jpg -set filename:f "%%t_%%wx%%h" -quality 100 "%%[filename:f].jpg"
That'll make, for example, "mississippi.jpg" into "mississippi_720x480.jpg", "illinois.jpg" into "illinois_480x720.jpg", etc.

Keep in mind that it does a conversion. Even with "-quality 100" you're going to change the input JPGs a tiny bit, so if you only want to rename the files, you'll probably have to work the output of the "identify" command into a loop based on what snibgo described above.

Also, if you run the command a second time in the same directory, each of those new output JPGs will get processed again. You might want to send the results of your command to a new directory to get them away from the originals. Create the output directory in your batch file and add it to the command something like this...

Code: Select all

mkdir donefiles

convert *.jpg -set filename:f "donefiles/%%t_%%wx%%h" -quality 100 "%%[filename:f].jpg"
Use only single percent signs "%" if you're doing it from the command line.

EDITED TO ADD: If you are just renaming, it may even be more efficient to use another tool altogether. There are probably several Windows utilities that can do the job. One I'm familiar with that seems to work especially well for bulk renaming is IrfanView. It allows you to insert variables like width and height, file size, resolution, various pieces of EXIF information, etc. into a naming template, then run it on individual files or entire directories.
AlexNovak
Posts: 3
Joined: 2016-04-18T05:36:43-07:00
Authentication code: 1151

Re: Add dimention into filename

Post by AlexNovak »

snibgo, GeeMack, thanks a lot!
Post Reply