Page 1 of 1

Is it possible to access in a Image Stack (parentheses) the dimensions of an image outside of the Image Stack?

Posted: 2018-01-14T13:47:23-07:00
by AVoeee
Hello,
is there a way to access the dimensions of an image which is outside of an Image Stack while you're inside of an Image Stack?

Here is my specific case:
I want to place a picture over another picture. The overhead image may be rotated, but it should always scaled so that it has the maximum possible dimensions relative to the underlying image.

This may look like this:
https://imgur.com/a/C1t3w
https://imgur.com/a/lYbZ6

The following code was executed in Bash on a Ubuntu using ImageMagick 6.8.9-9:

Code: Select all

size=`convert pic.png -density 300 -format "%wx%h" info:`
convert pic.png -density 300 \( top.png -rotate 35 -gravity center -resize "$size" \) -compose over -composite "result.jpg"
While inside of the Image Stack (parentheses), Attribute Percent Escapes like "%wx%h" refer to the current picture "top.png". So as far as I know, it is necessary split the action into two tasks.

Therefore I'm curious if it is possible to do something like this:

Code: Select all

convert pic.png -density 300 \( top.png -rotate 35 -gravity center -resize GET_DIMENSIONS_OF_PICTURE.PNG \) -compose over -composite "result.jpg"
Best regard
AVoeee

Re: Is it possible to access in a Image Stack (parentheses) the dimensions of an image outside of the Image Stack?

Posted: 2018-01-14T13:57:29-07:00
by fmw42
This only works in IM 7. The resize argument in IM 6 can only come from a predetermined variable as you did above and not from an in-line -set option:xxx.

Code: Select all

magick pic.png -set option:dim "%wx%h" \( top.png -rotate 35 -gravity center -resize "%[dim]" \) -compose over -composite -density 300 "result.jpg"

Re: Is it possible to access in a Image Stack (parentheses) the dimensions of an image outside of the Image Stack?

Posted: 2018-01-14T18:05:42-07:00
by GeeMack
AVoeee wrote: 2018-01-14T13:47:23-07:00I want to place a picture over another picture. The overhead image may be rotated, but it should always scaled so that it has the maximum possible dimensions relative to the underlying image. [...] Therefore I'm curious if it is possible to do something like this:

Code: Select all

convert pic.png -density 300 \( top.png -rotate 35 -gravity center -resize GET_DIMENSIONS_OF_PICTURE.PNG \) -compose over -composite "result.jpg"
--- EDITED TO REPAIR BROKEN COMMAND ---

There is a way to get that result with IM6, but not by moving values inside the parentheses There are less complicated methods using IM7 as fmw42 mentioned. With IM6 you can try a command like this...

Code: Select all

convert pic.png -density 300 -gravity center -background none \( top.png -rotate 35 \) \
 +distort SRT "%[fx:(u.w<v.w&&u.h<v.h)&&t?min(min(u.w/v.w,v.w/u.w),min(u.h/v.h,v.h/u.h)):1] 0" -shave 1x1 \
 +distort SRT "%[fx:(u.w>v.w||u.h>v.h)&&t?min(max(u.w/v.w,v.w/u.w),max(u.h/v.h,v.h/u.h)):1] 0" -shave 1x1 \
 -compose over -composite "result.jpg"
That reads in your input image and sets the density, gravity, and background color. Then inside the parentheses it reads in your overlay image and rotates it 35 degrees.

After that are two "+distort" operations. Both leave the main input image as-is and conditionally scale the overlay image. The first "+distort" shrinks the overlay if it's larger than the input image. The second enlarges the overlay if it's smaller than the main input. In either case the overlay is scaled to the maximum dimensions that fit within the main input image.

Note that using "+distort" adds one pixel to every edge, so those get removed with the "-shave 1x1" after each operation.

Re: Is it possible to access in a Image Stack (parentheses) the dimensions of an image outside of the Image Stack?

Posted: 2018-01-14T19:55:04-07:00
by fmw42
Nice catch, GeeMack. I forgot about using distort SRT.