Calculating with varaiables

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
Videomajk
Posts: 14
Joined: 2017-02-18T07:46:04-07:00
Authentication code: 1151

Calculating with varaiables

Post by Videomajk »

Hello
I have a question regarding how to use parameters definde with -set option together with fx percent escapes.

This is an example from http://www.imagemagick.org/Usage/distorts/#distort:

Code: Select all

convert rose: -set option:rw %w -set option:rh %h +delete \
           logo: -alpha set -virtual-pixel transparent \
           +distort Affine '0,0 0,0     %w,0 %[rw],0   0,%h  0,%[rh]' \
           +repage logo_sized_as_rose.png
This example saves the dimensions from one image and resize another image to match the size of the first one.

The width and height for the original image is first defined in the first line into the parameters rw and rh, and then used in the distort command by a percent escape: %[rw] and %[rh].

OK, now to my question. Let's say I would like the new image to be twice the size of the original image. Is it possible to use the rh and rh parameters in an fx commands, something like this:

Code: Select all

 +distort Affine '0,0 0,0     %w,0 %[fx:rw*2],0   0,%h  0,%[fx:rh*2]'
I've tried this particular way of writning, but it doesn't work. Is there a way of doing this?

I'm using IM 6.9.7 on Win 10
Last edited by Videomajk on 2017-02-26T13:30:24-07:00, edited 2 times in total.
------------------------------------------------------------------------
Imagemagick v 7.0.4 | Windows 10
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Aclculating with varaiables

Post by fmw42 »

Do the calculations in the -set option part.

-set option:rw "%[fx:w*2]"

Code: Select all

convert rose: -set option:rw  "%[fx:w*2]" -set option:rh  "%[fx:h*2]" +delete \
           logo: -alpha set -virtual-pixel transparent \
           +distort Affine '0,0 0,0     %w,0 %[rw],0   0,%h  0,%[rh]' \
           +repage logo_sized_as_rose.png
Videomajk
Posts: 14
Joined: 2017-02-18T07:46:04-07:00
Authentication code: 1151

Re: Aclculating with varaiables

Post by Videomajk »

Hello and thank you.
I have figured that out, but If I want to do something more complicated than this example, where I need different fractions of the rh and rw for different part of the image. Is the only way to do that to set different parameters with -set:option?
------------------------------------------------------------------------
Imagemagick v 7.0.4 | Windows 10
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Aclculating with varaiables

Post by GeeMack »

Videomajk wrote: 2017-02-26T13:00:11-07:00... but If I want to do something more complicated than this example, where I need different fractions of the rh and rw for different part of the image. Is the only way to do that to set different parameters with -set:option?
You should be able to do those calculations directly in the "-distort" operation. Something like...

Code: Select all

... +distort Affine '0,0 0,0     %[w],0 %[fx:w*2],0   0,%[h]  0,%[fx:h/3]' ...
These sorts of calculations can only be done in limited places with IM6, "-set", "-format", and "-distort" probably are the most commonly used. If you have a way to migrate to a newer version, you can use "%[fx:...]" calculations almost anywhere with IM7.
Videomajk
Posts: 14
Joined: 2017-02-18T07:46:04-07:00
Authentication code: 1151

Re: Calculating with varaiables

Post by Videomajk »

Okey, then I might concider migrating to IM7. My problem is that I have a Lightroom plugin that not yet support IM7 and I doubt that I can have both versions installed at the same time.
------------------------------------------------------------------------
Imagemagick v 7.0.4 | Windows 10
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Calculating with varaiables

Post by GeeMack »

Videomajk wrote: 2017-02-26T14:40:10-07:00Okey, then I might concider migrating to IM7. My problem is that I have a Lightroom plugin that not yet support IM7 and I doubt that I can have both versions installed at the same time.
I have IM6 and IM7 both installed on my Windows 10 64 machine, and both work perfectly. I installed them in different folders, of course, something like "C:\Program Files\ImageMagick\" and "C:\Program Files\ImageMagick6\". I made a point of using the "static" version of each so they don't have any shared or common libraries. The "portable" versions would probably work that way, too. I'm not sure how it might act with the "dll" versions.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Calculating with varaiables

Post by snibgo »

Videomajk wrote:I'm using IM 6.9.7 on Win 10
I'm on Windows 8.1. Like GeeMack, I have multiple version of IM installed, and they work fine.

But I don't use Lightroom. I suppose that uses the first IM on the system path, or in some declared directory, or something.
snibgo's IM pages: im.snibgo.com
Videomajk
Posts: 14
Joined: 2017-02-18T07:46:04-07:00
Authentication code: 1151

Re: Calculating with varaiables

Post by Videomajk »

snibgo wrote: 2017-02-26T14:58:11-07:00 But I don't use Lightroom. I suppose that uses the first IM on the system path, or in some declared directory, or something.
In the plugin I'm using (LR/Mogfrify 2) I need to point out the path to mogrify.exe, so I suppose that won't be a problem. I'll try to install IM 7. It seems to be better in many ways.

Thanks!
------------------------------------------------------------------------
Imagemagick v 7.0.4 | Windows 10
Post Reply