Define var and use it later

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
vadus

Define var and use it later

Post by vadus »

May be this are basics, but i can not get it work:

i need to "remeber" image geometrics %w, %h to use them later on a different image in a convert string. Is this possible?

For example:
convert [....] \( -size 800x600 xc:none [....] -annotate +0+0 'Random Text' -trim \( -size '%[fx:w]x%[fx:h]' [....] \) -compose SrcIn -composite\) -compose Over -composite [....]

So instead of wrong? writing -size '%[fx:w]x%[fx:h]' there should be something that would have been defined previously (in a var?) and get the image size after -trim

And why, if i use -fx 'Xi=w; Yi=h;' it is not possible to use Xi or Yi somewhere later on? Or is it?

The convert string, which is generated dynamically is ok. If i manually write e.g. -size 200x40 it generates in general what is intended, but not exact, since exact width and height of the previous image are needed... Any ideas?

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

Re: Define var and use it later

Post by fmw42 »

ww=`convert image.jpg -ping "%w" info:`
hh=`convert image.jpg -ping "%h" info:`
ww2=`convert image.jpg -ping "%[fx:$ww/2]" info:`
hh2=`convert image.jpg -ping "%[fx:$ww/2]" info:`

or

ww2=`convert image.jpg -ping "%[fx:w/2]" info:`
hh2=`convert image.jpg -ping "%[fx:h/2]" info:`

convert image.jpg -resize ${ww2}x${hh2} image2.jpg

resizes the image to half its size in each dimension
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Define var and use it later

Post by anthony »

-size currently can not take a % escape.

However -set size can, and does the same thing.
You will need to specify which image you want the size from.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Define var and use it later

Post by fmw42 »

Anthony, do you have a link to examples using -set size?

I did not know was available nor what it can do and what options allow its use?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Define var and use it later

Post by anthony »

Applogies I looked up my notes it is -set option:size

Code: Select all

convert rose:  -set option:size '%wx%h' gradient: -delete 0 show:

NOTE I have never advocated this on IM examples as I would prefer to get a more global method of adding percent-like escapes to ALL arguments, rather than relying on this method. The main stopping point for that is the use of '%' for escapes AND percentage values!!!!!

The ideal solution would be to use something other than '%' for escapes!


Actually ALL -set arguments allow the use of percent escapes, most 'settings' can be defined using -set option:??? This is known and has been used before.

For example using -bias with -convolve to add or subtract a calculated value...

Code: Select all

   convert rose:  -set option:bias '%[fx:-QuantumRange/2]' -convolve 1 show:
   convert rose:  -set option:bias '%[fx:-QuantumRange/2]' -convolve 1 show:
Also each 'value' is actually stored separatally into each image.
As such you can actyally 'set' a different value for each image!
For example set the bias according to the current image number or 'scene'

convert rose: \( -clone 0,0,0,0,0,0,0,0,0,0,0 \) \
-set option:bias '%[fx:t/12*QuantumRange]' -convolve 1 \
-set delay 20 -loop 0 gif:- | animate -

That works.. But if I replace the '12' with 'n' for number of images in in the current image sequence, it will fail!

The problem here is internal to IM. The scene number is stored in each image
but the total number of images is not and IM is performing the operations on each image one image at a time so 'n' is always 1 in the above. So you can not divide by 'n' and get the correct result! :-(

-fx 'n' gets the correct value -set ... '%[fx:n]' does not.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply