set a variables with the width and height of a image in dos

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
cbonallo
Posts: 15
Joined: 2010-09-27T11:24:34-07:00
Authentication code: 8675308

set a variables with the width and height of a image in dos

Post by cbonallo »

I am starting a new thread because I was asked to.
anthony wrote:Don't resurrect old threads especially ones 6 years old!

See IM examples, Windows Usage
http://www.imagemagick.org/Usage/windows/

This was not available when the original discussion was written.
I have searched the examples and documentation and believe I am very close. But, I do not understand something about how DOS uses quotes or ImageMagick syntax. I have working IM batches that I copied the syntax from originally.

I use Watch Directory as my service to set off the process. Due to that certain quotes have to stay in place for IM to get the passed parameters. ImageMagick requires certain things in quotes to recognize the symbols but I think DOS is also interpreting all these quotes. I have tried without single quotes but it assigns each of the code pieces in turn to the variable, not the result of the identify command. Any idea why this is not working or if there is a dos way to get this info from a jpg so I don't have to use the ImageMagick symbols to get it? Thanks for your help!

FOR /F %%x IN ('"C:\Program Files\ImageMagick-6.5.6-Q8\identify.exe" -ping -format "%%w" "%WD_FILE%"') DO SET IMGWIDTH=%%x
FOR /F %%y IN ('"C:\Program Files\ImageMagick-6.5.6-Q8\identify.exe" -ping -format "%%h" "%WD_FILE%"') DO SET IMGHEIGHT=%%y

IF %IMGWIDTH% LSS %IMGHEIGHT% ('"C:\Program Files\ImageMagick-6.5.6-Q8\convert.exe" "%WD_FILE%" -gravity center -extent 1000x1000 "\\harvardtest2\webimages\%WD_FILE_B%.jpg"')
IF %IMGHEIGHT% LSS %IMGWIDTH% ('"C:\Program Files\ImageMagick-6.5.6-Q8\convert.exe" "%WD_FILE%" -gravity center -extent 1500x1500 "\\harvardtest2\webimages\%WD_FILE_B%.jpg"')

Thank you for your time and help!

Cory Bonallo
User avatar
whugemann
Posts: 289
Joined: 2011-03-28T07:11:31-07:00
Authentication code: 8675308
Location: Münster, Germany 52°N,7.6°E

Re: set a variables with the width and height of a image in

Post by whugemann »

FOR /F %%x IN ('"C:\Program Files\ImageMagick-6.5.6-Q8\identify.exe" -ping -format "%%w" "%WD_FILE%"') DO SET IMGWIDTH=%%x
FOR /F %%y IN ('"C:\Program Files\ImageMagick-6.5.6-Q8\identify.exe" -ping -format "%%h" "%WD_FILE%"') DO SET IMGHEIGHT=%%y
This should work just fine, but this
IF %IMGWIDTH% LSS %IMGHEIGHT% ('"C:\Program Files\ImageMagick-6.5.6-Q8\convert.exe" "%WD_FILE%" -gravity center -extent 1000x1000 "\\harvardtest2\webimages\%WD_FILE_B%.jpg"')
IF %IMGHEIGHT% LSS %IMGWIDTH% ('"C:\Program Files\ImageMagick-6.5.6-Q8\convert.exe" "%WD_FILE%" -gravity center -extent 1500x1500 "\\harvardtest2\webimages\%WD_FILE_B%.jpg"')
should rather be

Code: Select all

IF %IMGWIDTH% LSS %IMGHEIGHT% "C:\Program Files\ImageMagick-6.5.6-Q8\convert.exe" "%WD_FILE%" -gravity center -extent 1000x1000 "\\harvardtest2\webimages\%WD_FILE_B%.jpg" 
IF %IMGHEIGHT% LSS %IMGWIDTH% "C:\Program Files\ImageMagick-6.5.6-Q8\convert.exe" "%WD_FILE%" -gravity center -extent 1500x1500 "\\harvardtest2\webimages\%WD_FILE_B%.jpg"
There is no need for single quotes or parantheses here (although the parantheses would do no harm). The single quotes are specific for the FOR /F command; they are not used anywhere else in DOS.
Wolfgang Hugemann
cbonallo
Posts: 15
Joined: 2010-09-27T11:24:34-07:00
Authentication code: 8675308

Re: set a variables with the width and height of a image in

Post by cbonallo »

I did try that but it still would not work. The parser was getting confused on the FOR command with all those quotes. I had to use a temp file for the value but I am concerned that it will be over written when running too quickly and the proper value will not be there at the proper time.

This is working:

Code: Select all

"C:\Program Files\ImageMagick-6.5.6-Q8\identify.exe" -ping -format "%%w" "C:\catalog\zz05020011c.jpg" > "C:\tempfile_width.txt"
"C:\Program Files\ImageMagick-6.5.6-Q8\identify.exe" -ping -format "%%h" "C:\catalog\zz05020011c.jpg" > "C:\tempfile_height.txt"
FOR /F "usebackq delims=" %%x IN (C:\tempfile_width.txt)  DO SET IMGWIDTH=%%x
FOR /F "usebackq delims=" %%y IN (C:\tempfile_height.txt)  DO SET IMGHEIGHT=%%y 

echo %IMGWIDTH%
echo %IMGHEIGHT%
IF %IMGWIDTH% LSS %IMGHEIGHT% "C:\Program Files\ImageMagick-6.5.6-Q8\convert.exe" "C:\catalog\zz05020011c.jpg" -gravity center -extent %IMGHEIGHT%x%IMGHEIGHT% "\\harvardtest2\webimages\zz05020011c.jpg" 
IF %IMGHEIGHT% LSS %IMGWIDTH% "C:\Program Files\ImageMagick-6.5.6-Q8\convert.exe" "C:\catalog\zz05020011c.jpg" -gravity center -extent %IMGWIDTH%x%IMGWIDTH% "\\harvardtest2\webimages\zz05020011c.jpg"
User avatar
whugemann
Posts: 289
Joined: 2011-03-28T07:11:31-07:00
Authentication code: 8675308
Location: Münster, Germany 52°N,7.6°E

Re: set a variables with the width and height of a image in

Post by whugemann »

I did try that but it still would not work. The parser was getting confused on the FOR command with all those quotes.
No, It never does, unless the expression is grammatically incorrect. The DOS batch language may be very crude, but it definitively functions flawless.
I had to use a temp file for the value but I am concerned that it will be over written when running too quickly and the proper value will not be there at the proper time.
No. You can rely on that every statement is finished before the next one starts. My DOS batch scripts are dumping a lot into text files, because this way of proceeding often has advantages. I never met any problems with that.
Wolfgang Hugemann
Post Reply