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

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
AVoeee
Posts: 15
Joined: 2018-01-14T12:17:32-07:00
Authentication code: 1152

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

Post by AVoeee » 2018-01-14T13:47:23-07:00

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

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

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

Post by fmw42 » 2018-01-14T13:57:29-07:00

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"

User avatar
GeeMack
Posts: 531
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

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

Post by GeeMack » 2018-01-14T18:05:42-07:00

AVoeee wrote:
2018-01-14T13:47:23-07:00
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. [...] 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.
Last edited by GeeMack on 2018-01-15T13:13:37-07:00, edited 1 time in total.

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

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

Post by fmw42 » 2018-01-14T19:55:04-07:00

Nice catch, GeeMack. I forgot about using distort SRT.

Post Reply