Using mathematical operators in commands?

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
WorldOfDepth
Posts: 7
Joined: 2019-08-14T01:23:15-07:00
Authentication code: 1152

Using mathematical operators in commands?

Post by WorldOfDepth » 2019-08-14T01:44:22-07:00

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!

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

Re: Using mathematical operators in commands?

Post by snibgo » 2019-08-14T04:26:27-07:00

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.
snibgo's IM pages: im.snibgo.com

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

Re: Using mathematical operators in commands?

Post by fmw42 » 2019-08-14T09:02:07-07:00

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

WorldOfDepth
Posts: 7
Joined: 2019-08-14T01:23:15-07:00
Authentication code: 1152

Re: Using mathematical operators in commands?

Post by WorldOfDepth » 2019-08-14T09:19:16-07:00

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?

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

Re: Using mathematical operators in commands?

Post by snibgo » 2019-08-14T09:35:07-07:00

Please show the exact command you used, and the exact error message.
snibgo's IM pages: im.snibgo.com

WorldOfDepth
Posts: 7
Joined: 2019-08-14T01:23:15-07:00
Authentication code: 1152

Re: Using mathematical operators in commands?

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

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.

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

Re: Using mathematical operators in commands?

Post by fmw42 » 2019-08-14T09:49:05-07:00

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

WorldOfDepth
Posts: 7
Joined: 2019-08-14T01:23:15-07:00
Authentication code: 1152

Re: Using mathematical operators in commands?

Post by WorldOfDepth » 2019-08-14T09:52:41-07:00

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

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

Re: Using mathematical operators in commands?

Post by fmw42 » 2019-08-14T10:37:53-07:00

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

WorldOfDepth
Posts: 7
Joined: 2019-08-14T01:23:15-07:00
Authentication code: 1152

Re: Using mathematical operators in commands?

Post by WorldOfDepth » 2019-08-14T10:46:46-07:00

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.

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

Re: Using mathematical operators in commands?

Post by fmw42 » 2019-08-14T10:53:50-07:00

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 \

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

Re: Using mathematical operators in commands?

Post by snibgo » 2019-08-14T10:54:09-07:00

WorldOfDepth wrote:convert input.jpg -resize %[fx:20^2] output.jpg
I used "magick", not "convert". Try again with "magick".
snibgo's IM pages: im.snibgo.com

WorldOfDepth
Posts: 7
Joined: 2019-08-14T01:23:15-07:00
Authentication code: 1152

Re: Using mathematical operators in commands?

Post by WorldOfDepth » 2019-08-14T11:01:19-07:00

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.

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

Re: Using mathematical operators in commands?

Post by fmw42 » 2019-08-14T11:47:52-07:00

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.

WorldOfDepth
Posts: 7
Joined: 2019-08-14T01:23:15-07:00
Authentication code: 1152

Re: Using mathematical operators in commands?

Post by WorldOfDepth » 2019-08-14T12:11:00-07:00

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.

Post Reply