Page 1 of 1
set a variable with the width of a dynamic image
Posted: 2009-03-11T20:56:15-07:00
by minipra
| Hi,
| How can I set a variable with the width of a dynamic image in a windows batch program..
|
| set x=identify -format '%w' tree.gif
|
| Thank you
| mini
|
Re: set a variable with the width of a dynamic image
Posted: 2009-03-12T06:49:04-07:00
by el_supremo
This will do it:
Code: Select all
for /f %A in ('identify -format "%w" tree.gif') do set x=%A
Pete
Re: set a variable with the width of a dynamic image
Posted: 2009-03-12T21:38:58-07:00
by minipra
el_supremo wrote:This will do it:
Code: Select all
for /f %A in ('identify -format "%w" tree.gif') do set x=%A
Pete
Thank you so much ... it works !!
It worked in command line , but it doesnt work in a batch program .. Iam new to this please help ! Thank you !!
Re: set a variable with the width of a dynamic image
Posted: 2009-03-13T09:59:02-07:00
by el_supremo
When you type a command interactively a variable is typed as %A but in a batch file you have to escape all percent symbols with another one %%A, so in a batch file your command must be:
Code: Select all
for /f %%A in ('identify -format "%%w" tree.gif') do set x=%%A
Pete
Re: set a variable with the width of a dynamic image
Posted: 2009-03-15T22:18:04-07:00
by anthony
thank you for that, I have noted it in IM Example Usage, under api's windows
http://www.imagemagick.org/Usage/api/#windows
However I think only the %w in the identify needs to have the percent symbol doubled. The others should be single percents as in those places you want the DOS usage of the percent meta-character.
that is
Code: Select all
for /f %A in ('identify -format "%%w" tree.gif') do set x=%A
Can someone please check and report if that is the correct method of the above. Rememeber I am only reporting this to others. I don't use it myself.
Also not that you can use IM as a floating point maths calculator for scripts.
For example, to calculate PI you can use....
Code: Select all
convert xc: -format '%[fx:atan(1)*4]' info:
don't forget to double that percent and change the quotes for DOS useage!
Re: set a variable with the width of a dynamic image
Posted: 2009-03-16T08:34:35-07:00
by el_supremo
for /f %A in ('identify -format "%%w" tree.gif') do set x=%A
Can someone please check and report if that is the correct method of the above
No, it fails with a syntax error. The A variable must be written as %%A.
But if later in the same batch file you wish to refer to the content of the variable x, it must be referenced as %x% - not %%x%%
See
http://www.computerhope.com/forhlp.htm for some info.
Pete
Re: set a variable with the width of a dynamic image
Posted: 2009-03-16T18:44:39-07:00
by anthony
I never seen %x% syntax, but I would not be surprised that that is the actual way DOS scripts substitute there variables.
Can you provide a small but complete working example, both setting a variable
then using it, which I can list in IM examples.
It does not have to be specifically useful, but it should be complete.
For example...
Say assign an image name
assign that images size to a variable
create a new image the same size using the variable
PS: how do your comment DOS scripts?
PPS: does DOS have any native, or standard method to do simple maths?
PPPS: if you like, take a look at IM Examples, API's windows, and mail me
a update that should make it more sensible and usable to window users.
Contact me privately and I'll enable you access to the 'test' server I use for checking before uploading to the official web pages.
Re: set a variable with the width of a dynamic image
Posted: 2009-03-17T16:37:21-07:00
by el_supremo
This example might do for a start. It is a batch command which resizes each JPG in a directory down to 40% and saves that file in the current directory with "_40" appended to the filename. It then cuts that in half and saves it to a subdirectory named "thumb_20" and then cuts the image in half again and saves it to the "thumb_10" subdirectory.
The name of the dependent variable "F" in the for loop is case-sensitive. When ~n is put in front of a variable it returns the filename part and ~x returns the extension so if F is "image.jpg" then ~nF is "image" and "~xF" will be ".jpg".
Code: Select all
rem Create medium, small and thumbnail images from an original
for %%F in (*.jpg) do imconvert %%F -resize 40%% -write %%~nF_40%%~xF -resize 50%% -write thumb_20\%%F -resize 50%% thumb_10\%%F
PS: how do your comment DOS scripts?
Use the REM command as in the above example.
PPS: does DOS have any native, or standard method to do simple maths?
I'm not aware of one but I don't use DOS batch very much at all - I prefer TCL for that sort of thing.
Pete
Re: set a variable with the width of a dynamic image
Posted: 2009-03-17T17:21:59-07:00
by anthony
Nice example I was hoping for one that sets a variable then uses it on the next line.
Also it does not show the %x% syntax you mentioned before but a %%F
syntax. Seems very confusing. How does it determine if %% means one percent or a variable substitution?
Simplier is better.
Re: set a variable with the width of a dynamic image
Posted: 2009-03-17T19:13:44-07:00
by anthony
I have created an example DOS script based on a script found in another discussion
Code: Select all
@ECHO OFF
:: Set some variables
SET INPUTFILE=%1
:: Fetch and set image size
FOR /F %%x IN ('identify -format "%%w" %INPUTFILE%') DO SET IMGWIDTH=%%x
FOR /F %%x IN ('identify -format "%%h" %INPUTFILE%') DO SET IMGHEIGHT=%%x
:: Append a black spacing image and save into temporary file
convert -size %IMGWIDTH%x5 xc:black %TEMP%\temp.png
As mentioned before if you (or someone else) would like to update IM Examples, they are welcome to submit them, and get credit for it.
I am sorry that it is not setup in the form of a Wiki, but I have automated scripts to regenerate examples from the actual code in the HTML, and that is something Wiki's do not seem to be able to do.
Re: set a variable with the width of a dynamic image
Posted: 2009-03-21T10:20:36-07:00
by el_supremo
Here's a slightly non-trivial example of a windows batch script. I found that the SET command can be used to do arithmetic computations.
It figures out the dimensions of an image and draws a red circle in the centre of the image. The diameter of the circle is half the shorter side of the image.
I've set it up so that it calls identify once to get the width and height of the image which requires that it call another batch script (because the FOR statement only allows one statement in the loop).
The script which just sets the variables for width and height is called setdim.bat:
Code: Select all
@echo off
rem this is setdim.bat
set IMGWIDTH=%1
set IMGHEIGHT=%2
@echo on
and the main script is forset.bat:
Code: Select all
@rem set the name of the input and output images
set image="logo:"
set output="logo_circle.gif"
@rem set IMGWIDTH and IMGHEIGHT using setdim.bat
@rem This sets variable A to the width and implicitly sets B to the height
@rem A and B are then passed to setdim to set the environment variables
FOR /F "tokens=1,2" %%A IN ('identify -format "%%w %%h" %image%') DO call setdim %%A %%B
@rem Assume that the height is smaller
set dim=%IMGHEIGHT%
@rem if the width is smaller, change dim
if %IMGWIDTH% LSS %IMGHEIGHT% then set dim=%IMGWIDTH%
@rem Set the radius - /A is required so that the computation is done
set /A rad=%dim%/4
@rem Compute the coordinate of the centre of the image
set /A cx=%IMGWIDTH%/2
set /A cy=%IMGHEIGHT%/2
@rem and the x coord of a point on the circumference of the circle
set /A px=%cx%+%rad%
@rem now draw the circle
imconvert %image% -fill red -draw "circle %cx%,%cy% %px%,%cy%" %output%
Pete