set a variable with the width of a dynamic image
set a variable with the width of a dynamic image
| 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
|
| 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
|
-
- Posts: 1015
- Joined: 2005-03-21T21:16:57-07:00
Re: set a variable with the width of a dynamic image
This will do it:
Pete
Code: Select all
for /f %A in ('identify -format "%w" tree.gif') do set x=%A
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
See my message in this topic for a link to a zip of all the files.
Re: set a variable with the width of a dynamic image
Thank you so much ... it works !!el_supremo wrote:This will do it:PeteCode: Select all
for /f %A in ('identify -format "%w" tree.gif') do set x=%A
It worked in command line , but it doesnt work in a batch program .. Iam new to this please help ! Thank you !!
-
- Posts: 1015
- Joined: 2005-03-21T21:16:57-07:00
Re: set a variable with the width of a dynamic image
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:
Pete
Code: Select all
for /f %%A in ('identify -format "%%w" tree.gif') do set x=%%A
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
See my message in this topic for a link to a zip of all the files.
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: set a variable with the width of a dynamic image
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
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....
don't forget to double that percent and change the quotes for DOS useage!
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
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:
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/
-
- Posts: 1015
- Joined: 2005-03-21T21:16:57-07:00
Re: set a variable with the width of a dynamic image
No, it fails with a syntax error. The A variable must be written as %%A.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
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
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
See my message in this topic for a link to a zip of all the files.
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: set a variable with the width of a dynamic image
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.
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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/
-
- Posts: 1015
- Joined: 2005-03-21T21:16:57-07:00
Re: set a variable with the width of a dynamic image
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".
Pete
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
Use the REM command as in the above example.PS: how do your comment DOS scripts?
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.PPS: does DOS have any native, or standard method to do simple maths?
Pete
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
See my message in this topic for a link to a zip of all the files.
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: set a variable with the width of a dynamic image
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.
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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: set a variable with the width of a dynamic image
I have created an example DOS script based on a script found in another discussion
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.
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
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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/
-
- Posts: 1015
- Joined: 2005-03-21T21:16:57-07:00
Re: set a variable with the width of a dynamic image
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:
and the main script is forset.bat:
Pete
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
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%
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
See my message in this topic for a link to a zip of all the files.