Page 1 of 2

Composite transparent layer and underlying layer

Posted: 2016-02-25T15:22:36-07:00
by buchert
Hi, I'm using ImageMagick CLI in LInux Mint.

I want a command that will combine a transparent image with borders:

Image

And layer this transparent image over another image, keeping the borders of the transparent image on top:

Image

I tried this:

Code: Select all

convert aaa.jpg  -draw "image over x,y 0,0 bbb.gif" result.png
aaa.jpg is the underlying image and bbb.gif is the transparent image with borders. The result doesn't work. It outputs the underlying image, slightly brightened but with no border.

Also, is there a command that will determine how large the transparent image needs to be to correctly overlap the underlying image, instead of having to modify the command to exact dimensions? I'd like the command to be automated, and "just work" no matter the dimensions of the images I use.

Re: Composite transparent layer and underlying layer

Posted: 2016-02-25T17:03:57-07:00
by fmw42
What version of Imagemagick are you using? Please provide your underlying image? Is it the same size as the frame? If not, that may cause problems

Try

Code: Select all

convert aaa.jpg bbb.gif -background black -gravity center -flatten result.png
or

Code: Select all

convert aaa.jpg bbb.gif -background black -gravity center -compose over -composite result.png
or

Code: Select all

convert bbb.gif aaa.jpg -background black -gravity center -compose dstover -composite result.png
if these do not help, then you need to compute the underlying image size and either crop or resize to the size of the frame image. That means that you will need to write several lines of code (script) to compute the dimension and do the crop or resize (or pad) and then overlay. If you are willing to crop or pad, then it can be done in one command line.

(untested)

Code: Select all

convert aaa.jpg bbb.gif \
\( -clone 1-alpha off -fill black -colorize 100 \) \
\( -clone 2 -clone 0 -gravity center -compose over -composite \) \
-delete 0,2 +swap -gravity center -compose over -composite result.png

Re: Composite transparent layer and underlying layer

Posted: 2016-02-25T17:21:43-07:00
by fmw42
See EDITED code above

Re: Composite transparent layer and underlying layer

Posted: 2016-02-25T17:34:28-07:00
by buchert
Thanks for your help!

Imagemagick version: 8:6.7.7.10-6ubun

Results of First command:

Image

Results of second command:

Image

Results of third command (this is the best):

Image


I tried this command:

Code: Select all

convert aaa.jpg bbb.gif \
\( -clone 1-alpha off -fill black -colorize 100 \) \
\( -clone 2 -clone 0 -gravity center -compose over -composite \) \
-delete 0,2 +swap -gravity center -compose over -composite result.png
And received this error:

Code: Select all

convert aaa.jpg bbb.gif \
> \( -clone 1-alpha off -fill black -colorize 100 \) \
> \( -clone 2 -clone 0 -gravity center -compose over -composite \) \
> -delete 0,2 +swap -gravity center -compose over -composite result.png
convert.im6: invalid argument for option `-clone': 1-alpha @ error/convert.c/ConvertImageCommand/943.

Re: Composite transparent layer and underlying layer

Posted: 2016-02-25T17:58:56-07:00
by fmw42
Sorry, typo --- missing space before -alpha off in second line. But I had hoped that the third one would work, anyway.

Code: Select all

convert aaa.jpg bbb.gif \
\( -clone 1 -alpha off -fill black -colorize 100 \) \
\( -clone 2 -clone 0 -gravity center -compose over -composite \) \
-delete 0,2 +swap -gravity center -compose over -composite result.png

Re: Composite transparent layer and underlying layer

Posted: 2016-02-26T00:51:15-07:00
by buchert
I tried the above and got this:

Image

This command produced the same results (the one I posted before was scaled by me to the size of the gif, which is why it looks better):

Code: Select all

convert bbb.gif aaa.jpg -background black -gravity center -compose dstover -composite result.jpg
Ideally I'd like a command that can automatically resize the underlying image to fit the dimensions of the overlying gif, or vice versa.

Re: Composite transparent layer and underlying layer

Posted: 2016-02-26T02:20:03-07:00
by snibgo
buchert wrote:Ideally I'd like a command that can automatically resize the underlying image to fit the dimensions of the overlying gif, or vice versa.
You have an image with a border around a transparent area. You can find the width and height of the area with "-alpha extract" and either "-trim" or "-format %@". So the required new size for the underlying image is at least those dimensions.

Re: Composite transparent layer and underlying layer

Posted: 2016-02-26T09:42:16-07:00
by fmw42
I do not see how you got a scale image from my command. If you provide the background image alone along with the frame image, we can test and give you better code.

Re: Composite transparent layer and underlying layer

Posted: 2016-02-26T10:45:07-07:00
by buchert
Ok! Here's the full scale .gif overlay:

Image

And the full scale background image:

Image

Re: Composite transparent layer and underlying layer

Posted: 2016-02-26T11:20:04-07:00
by fmw42
Try this:

Code: Select all

size=`convert mo9LQCx.gif -format "%wx%h" info:`
convert \( Ah4mtPw.jpg -resize $size^ -gravity center -extent $size \) mo9LQCx.gif -compose over -composite result.png

Re: Composite transparent layer and underlying layer

Posted: 2016-02-26T11:53:20-07:00
by buchert
Tx, that works! How can the command be modified so that the underlying image is resized to these exact dimensions: 1550x2067

If I could get the underlying image to resize to those exact dimensions then I wouldn't have to resize the image before compositing it with the .gif overlay.

Re: Composite transparent layer and underlying layer

Posted: 2016-02-26T14:08:24-07:00
by fmw42
buchert wrote:Tx, that works! How can the command be modified so that the underlying image is resized to these exact dimensions: 1550x2067

If I could get the underlying image to resize to those exact dimensions then I wouldn't have to resize the image before compositing it with the .gif overlay.
I do not understand. If you resize to exactly 1550x2067 using -resize 1550x2067!, it will distort the image because your image is a different aspect ratio. So what I have done is resized it so that the smaller dimension matches that of the frame image and then cropped off the excess with -extent.

Resizing ahead of the command is no different than letting the command resize it in the parenthesis operation and it adds a separate command.

Please clarify what you are wanting to do.

Re: Composite transparent layer and underlying layer

Posted: 2016-02-26T17:04:45-07:00
by buchert
Sorry for the confusion. It could be that what I want to do is impossible in one command.

In addition to compositing the background and gif images together, I'd also like the command to resize the background jpg image and/or the gif image so that the background jpg image will fit nicely within the borders (the transparent center) of the gif image.

Re: Composite transparent layer and underlying layer

Posted: 2016-02-26T17:15:34-07:00
by fmw42
try

Code: Select all

convert mo9LQCx.gif \( Ah4mtPw.jpg -resize 1550x2067^ -gravity center -extent 1550x2067 \) -gravity center -compose dstover -composite result.png
If you do not care about distorting the background image, then try

Code: Select all

convert mo9LQCx.gif  \( Ah4mtPw.jpg -resize 1550x2067! \) -gravity center -compose dstover -composite result.png

EDIT: corrected above

Re: Composite transparent layer and underlying layer

Posted: 2016-02-26T17:41:46-07:00
by buchert
Tx, they work nicely for background images which are 1550x2067 or close. But if I'm using an image with much different dimensions it would be ideal if the command could take account of the dimensions of the gif image and the background image and resize them both (if need be) to produce a composite where the background image fits fully within the transparent center of the gif image.

If this isn't possible then black padding around the background image would be acceptable.