Page 1 of 1

Using mathematical operators in commands?

Posted: 2019-08-14T01:44:22-07:00
by WorldOfDepth
Hi, sorry, this may be a simple question, but I cannot find the answer after lots of searching!

Is it possible to use a mathematical expression that would be evaluated and replaced by a number within a command? I see all the math operators available for the FX operator, but I don’t think that’s what I should be using here…

For example, if I wanted to resize an image to a width that is equal to 16 to the 3rd power:

Code: Select all

convert input.jpg -resize (16^3) output.jpg
This obviously doesn’t work, not only since the ^ character has a different meaning in the context of image size. Would this, or other math expressions, be possible via a variable of some sort? Or by using some alternate syntax or enclosure?

I need this for passing a command line argument to a shell alias—resizing to $1^3, for example.

Thank you for any help!

Re: Using mathematical operators in commands?

Posted: 2019-08-14T04:26:27-07:00
by snibgo
What version of IM, on what platform?

If you use IM V7, you can use "%[fx:...]" expressions such as:

Code: Select all

magick xc: -resize %[fx:16^^3] info:
This is Windows syntax, where caret ^ is a special character so must be escaped by ^. For bash, don't double the ^.

FX expressions can use variables defined with IM, such as w for image width.

Re: Using mathematical operators in commands?

Posted: 2019-08-14T09:02:07-07:00
by fmw42
snibgo shows how to do it in IM 7, which has more capabilities. In IM 6, you need to compute the final variable first, then use it in the resize such as

Code: Select all

val=$1
val2=$(convert xc: -format "%[fx:$val^3]" info:)
convert input.jpg -resize $val2 output.jpg
or

Code: Select all

val=$(convert xc: -format "%[fx:$1^3]" info:)
convert input.jpg -resize $val output.jpg

Re: Using mathematical operators in commands?

Posted: 2019-08-14T09:19:16-07:00
by WorldOfDepth
Thank you snibgo and fmw42!

Sorry for not including it—I'm using version 7.0.8-3 via Mac Terminal. I think the fx expression plus percent escape is what I want, but IM returns an "invalid argument" when I try %[fx:20^2], %[fx:20^^2], "%[fx:20^2]", "%[fx:20^^2]", or %\[fx:20^2\]… How should this be correctly formatted?

Re: Using mathematical operators in commands?

Posted: 2019-08-14T09:35:07-07:00
by snibgo
Please show the exact command you used, and the exact error message.

Re: Using mathematical operators in commands?

Posted: 2019-08-14T09:40:58-07:00
by WorldOfDepth

Code: Select all

convert input.jpg -resize %[fx:20^2] output.jpg
returns: "convert: invalid argument for option '-resize': %[fx:20^2] @ error/convert.c/ConvertImageCommand/2609"

Using double carets or quotes around it returns a similar error.

Re: Using mathematical operators in commands?

Posted: 2019-08-14T09:49:05-07:00
by fmw42
In Unix, you must enclose %[fx:20^2] in double quotes as per my example above.

convert input.jpg -resize "%[fx:20^2]" output.jpg

Re: Using mathematical operators in commands?

Posted: 2019-08-14T09:52:41-07:00
by WorldOfDepth
I did try that, but it returns the exact same error as before (the double quotes are not regurgitated in the error message).

Also don't work: single quotes; escaping the brackets and/or caret with backslashes

Re: Using mathematical operators in commands?

Posted: 2019-08-14T10:37:53-07:00
by fmw42
Sorry my typo, you can only do that in IM 7 as described above. But you do need the double quotes. In IM 6, you have to compute a variable, then use the variable in the -resize.

IM 7

Code: Select all

magick input.jpg -resize "%[fx:20^2]" output.jpg
IM6

Code: Select all

var=$(convert xc: -format "%[fx:20^2]" info:)
convert input -resize $var output.jpg

Re: Using mathematical operators in commands?

Posted: 2019-08-14T10:46:46-07:00
by WorldOfDepth
Thanks fmw42! Your code for IM6 works for me if I omit the usual backslash escapes for parentheses that I have to use within IM commands. The IM7 code won't work despite my version being 7.0.8-3.

Re: Using mathematical operators in commands?

Posted: 2019-08-14T10:53:50-07:00
by fmw42
I do not have \ on those parentheses. They are Unix parentheses not ImageMagick parentheses. In Unix for a computation using ImageMagick, you can use $(...) or backticks `...`

Imagemagick parentheses must include \( and \) and have spaces on each side.

If you are not using Unix as in Linux or Mac OSX, then for example in Windows, parentheses should not be escaped by \

Re: Using mathematical operators in commands?

Posted: 2019-08-14T10:54:09-07:00
by snibgo
WorldOfDepth wrote:convert input.jpg -resize %[fx:20^2] output.jpg
I used "magick", not "convert". Try again with "magick".

Re: Using mathematical operators in commands?

Posted: 2019-08-14T11:01:19-07:00
by WorldOfDepth
Snibgo, wow, I overlooked that, and indeed it works with magick, and even without double quotes! I'm just so used to convert and composite as my defaults. Thanks so much, and thanks fmw42 again for the option via a variable.

Re: Using mathematical operators in commands?

Posted: 2019-08-14T11:47:52-07:00
by fmw42
I do not understand. You need the double quotes on a Mac. Are you sure you are using Unix on a Mac and not some Windows emulator on the Mac.

Re: Using mathematical operators in commands?

Posted: 2019-08-14T12:11:00-07:00
by WorldOfDepth
Fmw42, I'm using the utility program Terminal in OSX Yosemite. The window is labeled bash. I believe it's Unix.

The %[fx:20^2] appears to works within a 'magick' command regardless of whether or not it's enclosed in double quotes.