Composite transparent layer and underlying layer

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?".
buchert
Posts: 36
Joined: 2015-02-13T11:15:29-07:00
Authentication code: 6789

Composite transparent layer and underlying layer

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Composite transparent layer and underlying layer

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Composite transparent layer and underlying layer

Post by fmw42 »

See EDITED code above
buchert
Posts: 36
Joined: 2015-02-13T11:15:29-07:00
Authentication code: 6789

Re: Composite transparent layer and underlying layer

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Composite transparent layer and underlying layer

Post 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
buchert
Posts: 36
Joined: 2015-02-13T11:15:29-07:00
Authentication code: 6789

Re: Composite transparent layer and underlying layer

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Composite transparent layer and underlying layer

Post 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.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Composite transparent layer and underlying layer

Post 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.
buchert
Posts: 36
Joined: 2015-02-13T11:15:29-07:00
Authentication code: 6789

Re: Composite transparent layer and underlying layer

Post by buchert »

Ok! Here's the full scale .gif overlay:

Image

And the full scale background image:

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

Re: Composite transparent layer and underlying layer

Post 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
buchert
Posts: 36
Joined: 2015-02-13T11:15:29-07:00
Authentication code: 6789

Re: Composite transparent layer and underlying layer

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Composite transparent layer and underlying layer

Post 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.
buchert
Posts: 36
Joined: 2015-02-13T11:15:29-07:00
Authentication code: 6789

Re: Composite transparent layer and underlying layer

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Composite transparent layer and underlying layer

Post 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
buchert
Posts: 36
Joined: 2015-02-13T11:15:29-07:00
Authentication code: 6789

Re: Composite transparent layer and underlying layer

Post 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.
Post Reply