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.
Do math with -set variables?
-
- Posts: 64
- Joined: 2017-10-03T10:39:52-07:00
- Authentication code: 1151
Re: Do math with -set variables?
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?
You may need a script, so what platform and language?
- GeeMack
- Posts: 718
- Joined: 2015-12-01T22:09:46-07:00
- Authentication code: 1151
- Location: Central Illinois, USA
Re: Do math with -set variables?
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.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.
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.
-
- Posts: 64
- Joined: 2017-10-03T10:39:52-07:00
- Authentication code: 1151
Re: Do math with -set variables?
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:GeeMack wrote: ↑2018-04-19T07:29:32-07:00You 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.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.
Code: Select all
magick "$file" -set ht '%h' -gravity north -trim -fill black -draw 'color 0,0 reset' -background white -extent x"%[ht]"
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
Thanks for this.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.
-
- Posts: 64
- Joined: 2017-10-03T10:39:52-07:00
- Authentication code: 1151
Re: Do math with -set variables?
Solved this bit: I need to reset -compose appropriately.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.
- GeeMack
- Posts: 718
- Joined: 2015-12-01T22:09:46-07:00
- Authentication code: 1151
- Location: Central Illinois, USA
Re: Do math with -set variables?
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...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.
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]"
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.
-
- Posts: 64
- Joined: 2017-10-03T10:39:52-07:00
- Authentication code: 1151
Re: Do math with -set variables?
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:00When 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...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.
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.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]"
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".
Yes, I've been looking at that, but my examples doesn't appear (because it's not possible, as you've pointed out).GeeMack wrote: ↑2018-04-19T15:08:48-07:00 See more about FX expressions and the internal variables and functions available at THIS link.