How to use image -format / info: in same command-line ?

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
Alexvb6
Posts: 49
Joined: 2012-10-12T16:50:15-07:00
Authentication code: 67789

How to use image -format / info: in same command-line ?

Post by Alexvb6 »

Hi,

Under Dos, with IM v7, sometimes I need to identify the Width of the image using convert, then use this variable later in my connvert command, without any pipe.
I'm a little lost on how about to perform this...

Here, I am trying to reduce image Width by its width minus 12 pixels, for example :
(I know my syntax is not the good one, and this code does not work...)

Code: Select all

convert ^
 c:\source.png
 -resize ([%w]-12)x100%
 c:\destination.png
PS: My example is only for "example purpose"... What I need is really a way to extract some data about the image thanks to the "Percent Escape Properties", and re-use this information later in my command-line.

Would someone have an idea ? The docs are not very "verbose" about it ;) http://www.imagemagick.org/Usage/basics/#identify_alt
Thank you (and sorry for this newb' question)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to use image -format / info: in same command-line ?

Post by fmw42 »

You cannot mix pixel width with percent width in the resize arguments. So you would have to use [fx:100*(w-12)/w]x100% or %[fx:w-12]x%[fx:h].

If you want to save a calculation, use -set option:size "%[fx:w-12]x%[fx:h]", then later -resize "%[size] in IM 7

IM 6 does not allow inline calculations to be used later for -resize or most other operators (except for -distort).

If using IM 7, then replace convert with magick (or convert.exe with magic.exe). In IM 6 be sure you are not confusing the Window convert.exe with the Imagemagick convert.exw.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to use image -format / info: in same command-line ?

Post by snibgo »

To resize the image so the width reduces by 12 and the height is unchanged, the obvious command, in v7, is:

Code: Select all

magick in.png -resize %[fx:w-12]x out.png
Note that for "-resize WxH", I have not supplied a height, so that isn't changed.

More generally, if you want to save a value for use later in the command, use "-set option", eg:

Code: Select all

magick in.png -set option:NewW %[fx:w-12] -resize %[NewW]x out.png
If using these in Windows BAT scripts, double every %.
snibgo's IM pages: im.snibgo.com
Alexvb6
Posts: 49
Joined: 2012-10-12T16:50:15-07:00
Authentication code: 67789

Re: How to use image -format / info: in same command-line ?

Post by Alexvb6 »

Hi Fred, Hi Snibgo,

Thank you very much : Using dynamic variables works perfectly in command-line : I really thank you for this, I was not able to make that work in the past !

However, As I am using the COM+ Object, unsing variables seems not possible using the 'Convert' tool (Even in command-line, dynamic variables only work with the "Magick" tool).
Do you confirm me that there is not way to use variables using "convert" ?

I will try to post that question in the "COM+ Object" forum, even if it is not very busy, sadly.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to use image -format / info: in same command-line ?

Post by snibgo »

IM V7 will accept an %[fx:...] expression in most places it expects a number, such as in "-resize".

IM v6 is more restricted. %[fx:...] expressions can only be used in certain places (like "-distort SRT"), but not in "-resize".

Of course, if you are using IM within a programming language, you can evaluate expressions in that language.
snibgo's IM pages: im.snibgo.com
Alexvb6
Posts: 49
Joined: 2012-10-12T16:50:15-07:00
Authentication code: 67789

Re: How to use image -format / info: in same command-line ?

Post by Alexvb6 »

Yes Snibgo, I am currently using VBScript to evaluate expressions, but between several IM command-lines.
In some cases, I could gain speed and improve performance by letting IM do simple computing in dynamic variables (such as for exemple dividing a number, or substracting ...)
Alexvb6
Posts: 49
Joined: 2012-10-12T16:50:15-07:00
Authentication code: 67789

Re: How to use image -format / info: in same command-line ?

Post by Alexvb6 »

FYI, the support for the "magick" command in the COM+ Object is planned to be added in the next release of ImageMagick, as I suggested it in this ticket :
https://github.com/ImageMagick/ImageMagick/issues/1083
Post Reply