Store results of "convert" to variable in windows batch script

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
ElPeta
Posts: 8
Joined: 2019-08-30T16:48:48-07:00
Authentication code: 1152

Store results of "convert" to variable in windows batch script

Post by ElPeta »

Hi all,

I'm working on a script that recursively adds a watermark to a series of images of different sizes in a folder. I need to modify the size of the watermark for each picture so that it is to the image it is being added to. To achieve that, I need to find out the width and height of the image, and then consequently modify the size. I know how to get the dimensions of the picture: I'm using these command lines:

To get the width:

Code: Select all

magick convert picture.jpg -ping -format %w info:
To get the height:

Code: Select all

magick convert picture.jpg -ping -format %h info:
If I use those same lines in command prompt, I do get the correct information (e.g. "5184" and "3456"). But what I need to do is store results in a variable within a batch file so that I can later use it to calculate the proportions of the watermark. Now, I am a newbie in batch scripting too, so I might be overlooking something very basic. Anyway, I tried it like this:

Code: Select all

set height=magick convert picture.jpg -ping -format %h info:
And then to show the contents of the variable "height":

Code: Select all

echo %height%
What I'm getting is this:
magick convert picture.jpg -ping -format

Seems like I'm only able to display the command, but not the results from running that command.

Can anyone help?

snibgo
Posts: 12863
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Store results of "convert" to variable in windows batch script

Post by snibgo »

A Windows BAT script:

Code: Select all

for /F "usebackq" %%L in (`magick ^
  identify -ping -format "width=%%w\nheight=%%h" ^
  picture.jpg`) do set %%L
This sets the variables named "width" and "height".
snibgo's IM pages: im.snibgo.com

User avatar
fmw42
Posts: 26250
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Store results of "convert" to variable in windows batch script

Post by fmw42 »

If on Imagemagick 7, use magick, not magick convert. It will invoke IM 6 behavior. Other IM 6 tools such as identify, do need to be prefaced by magick when using them in IM 7.

ElPeta
Posts: 8
Joined: 2019-08-30T16:48:48-07:00
Authentication code: 1152

Re: Store results of "convert" to variable in windows batch script

Post by ElPeta »

snibgo wrote:
2020-02-16T18:19:30-07:00
A Windows BAT script:

Code: Select all

for /F "usebackq" %%L in (`magick ^
  identify -ping -format "width=%%w\nheight=%%h" ^
  picture.jpg`) do set %%L
This sets the variables named "width" and "height".
Thanks so much, @snibgo!! That does precisely what I need.

Also, thanks for your input, Fred!!!

Post Reply