Page 1 of 1

Add dimention into filename

Posted: 2016-04-18T05:58:58-07:00
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

Re: Add dimention into filename

Posted: 2016-04-18T06:43:10-07:00
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

Re: Add dimention into filename

Posted: 2016-04-18T08:57:03-07:00
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.

Re: Add dimention into filename

Posted: 2016-04-18T09:01:35-07:00
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

Re: Add dimention into filename

Posted: 2016-04-18T13:29:40-07:00
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.

Re: Add dimention into filename

Posted: 2016-04-20T01:33:44-07:00
by AlexNovak
snibgo, GeeMack, thanks a lot!