Do math with -set variables?

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
muccigrosso
Posts: 64
Joined: 2017-10-03T10:39:52-07:00
Authentication code: 1151

Do math with -set variables?

Post by muccigrosso »

I'm saving an image height with `-set ht '%h'` and then using it later when I need to restore the image to its original height. What I tried to do was to add a border to the image where the height of the border is the difference between the original height and the current height. So I was hoping I could do something like `%[fx:ht-h]` but that doesn't work.

I'm pretty sure I can do what I want in another way, but is such math possible with variables created with -set?

TIA.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Do math with -set variables?

Post by Bonzo »

You should know by now muccigrosso; what version and what exact codes are you using?

You may need a script, so what platform and language?
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Do math with -set variables?

Post by GeeMack »

muccigrosso wrote: 2018-04-19T06:47:34-07:00I'm saving an image height with `-set ht '%h'` and then using it later when I need to restore the image to its original height. What I tried to do was to add a border to the image where the height of the border is the difference between the original height and the current height. So I was hoping I could do something like `%[fx:ht-h]` but that doesn't work.
You can set variables that way, but you can't use them inside following FX expressions. There are, however, several ways to carry that value along and use it later in the command. If you can provide a simple example of a complete command or a more detailed description of what you're trying to accomplish, someone can surely offer suggestions about how to do that.

Also, when you set a variable the way you've done, "-set ht '%h'", it will fail if you set it inside parentheses and try to access it outside the parentheses, or vice versa. A generally more useful way to set a variable is "-set option:ht '%h'". That way you can access it with "%[ht]" anywhere later in the command.
muccigrosso
Posts: 64
Joined: 2017-10-03T10:39:52-07:00
Authentication code: 1151

Re: Do math with -set variables?

Post by muccigrosso »

GeeMack wrote: 2018-04-19T07:29:32-07:00
muccigrosso wrote: 2018-04-19T06:47:34-07:00I'm saving an image height with `-set ht '%h'` and then using it later when I need to restore the image to its original height. What I tried to do was to add a border to the image where the height of the border is the difference between the original height and the current height. So I was hoping I could do something like `%[fx:ht-h]` but that doesn't work.
You can set variables that way, but you can't use them inside following FX expressions. There are, however, several ways to carry that value along and use it later in the command. If you can provide a simple example of a complete command or a more detailed description of what you're trying to accomplish, someone can surely offer suggestions about how to do that.
I'm doing this in a bash script on a Mac, so I can definitely get the value and save it, but I was hoping for a way that let me keep as much as possible in one IM command without having to write out a temp file. I enjoy trying to work through the code, which is why I limited my question to the ability to do math with variables (options?). However for a simplified version of what I'm doing more broadly, there's this:

Code: Select all

magick "$file" -set ht '%h' -gravity north -trim -fill black -draw 'color 0,0 reset' -background white -extent x"%[ht]"
There's more going on in my command, but it's that bit at the end that I'd like to do by adding a border instead of using extent, as here. I'm basically trimming down a file, making it black and then adding a white border on the top, so I need to know how big the border should be, hence the desire to somehow calculate the difference between the original height (assigned to ht) and the current height (which would be h, of course). The resultant file with be composited with the original.

In my much longer command, if I use extent to get the bigger image the entire resulting image is white, instead of retaining the black, so something else is going on there which leads me to try border.

PS In this simplified command, I get a white border on the left of the final image, in addition to the strip at top. I'm not sure why that's happening either.

Version: ImageMagick 7.0.7-28 Q16 x86_64 2018-03-26 http://www.imagemagick.org
Also, when you set a variable the way you've done, "-set ht '%h'", it will fail if you set it inside parentheses and try to access it outside the parentheses, or vice versa. A generally more useful way to set a variable is "-set option:ht '%h'". That way you can access it with "%[ht]" anywhere later in the command.
Thanks for this.
muccigrosso
Posts: 64
Joined: 2017-10-03T10:39:52-07:00
Authentication code: 1151

Re: Do math with -set variables?

Post by muccigrosso »

muccigrosso wrote: 2018-04-19T08:40:59-07:00 [In my much longer command, if I use extent to get the bigger image the entire resulting image is white, instead of retaining the black, so something else is going on there which leads me to try border.

PS In this simplified command, I get a white border on the left of the final image, in addition to the strip at top. I'm not sure why that's happening either.
Solved this bit: I need to reset -compose appropriately.
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Do math with -set variables?

Post by GeeMack »

muccigrosso wrote: 2018-04-19T08:40:59-07:00There's more going on in my command, but it's that bit at the end that I'd like to do by adding a border instead of using extent, as here. I'm basically trimming down a file, making it black and then adding a white border on the top, so I need to know how big the border should be, hence the desire to somehow calculate the difference between the original height (assigned to ht) and the current height (which would be h, of course). The resultant file with be composited with the original.
When you do a "-trim" operation, the image retains the pre-trimmed canvas dimensions in the variables "page.width" and "page.height" (... until you reset or change the canvas paging with "+repage", "-extent", etc.). So here's your example command and another way to write it which should give you the exact same result...

Code: Select all

magick "$file" -set ht '%h' -gravity north -trim \
   -fill black -draw 'color 0,0 reset' -background white -extent x"%[ht]"

magick "$file" -gravity north -trim \
   -fill black -draw 'color 0,0 reset' -background white -extent x"%[fx:page.height]"
You can't use your set variable "ht" within other FX expressions, but you can use that "page.height" variable in an FX calculation, like "%[fx:page.height-h]". That would subtract the current height of the image from the pre-trimmed page height.

If your command continues with other operations like distortions or composites, you may want to do a "+repage" to clear that paging information right after the "-extent".

See more about FX expressions and the internal variables and functions available at THIS link.
muccigrosso
Posts: 64
Joined: 2017-10-03T10:39:52-07:00
Authentication code: 1151

Re: Do math with -set variables?

Post by muccigrosso »

GeeMack wrote: 2018-04-19T15:08:48-07:00
muccigrosso wrote: 2018-04-19T08:40:59-07:00There's more going on in my command, but it's that bit at the end that I'd like to do by adding a border instead of using extent, as here. I'm basically trimming down a file, making it black and then adding a white border on the top, so I need to know how big the border should be, hence the desire to somehow calculate the difference between the original height (assigned to ht) and the current height (which would be h, of course). The resultant file with be composited with the original.
When you do a "-trim" operation, the image retains the pre-trimmed canvas dimensions in the variables "page.width" and "page.height" (... until you reset or change the canvas paging with "+repage", "-extent", etc.). So here's your example command and another way to write it which should give you the exact same result...

Code: Select all

magick "$file" -set ht '%h' -gravity north -trim \
   -fill black -draw 'color 0,0 reset' -background white -extent x"%[ht]"

magick "$file" -gravity north -trim \
   -fill black -draw 'color 0,0 reset' -background white -extent x"%[fx:page.height]"
You can't use your set variable "ht" within other FX expressions, but you can use that "page.height" variable in an FX calculation, like "%[fx:page.height-h]". That would subtract the current height of the image from the pre-trimmed page height.

If your command continues with other operations like distortions or composites, you may want to do a "+repage" to clear that paging information right after the "-extent".
Thanks for this. I always forget about repaging and I realized that might give me an out, but only after I was well into this process. I'll play with that some.
GeeMack wrote: 2018-04-19T15:08:48-07:00 See more about FX expressions and the internal variables and functions available at THIS link.
Yes, I've been looking at that, but my examples doesn't appear (because it's not possible, as you've pointed out).
Post Reply