Search code examples
imagemagickimagemagick-convert

Overlay PNGs with ImageMagick while keeping transparency


I have two images:

Image 1

In this image, the white region plus the white + pink region are transparent.

enter image description here

Image 2

enter image description here

GOAL

I want to merge both images (Image 1 in front, Image 2 behind) by:

  1. Keeping the transparent region from Image 1 so that Image 2 can be seen through the white mask.
  2. Having the chance to locate Image 2 by vertically centering the photo in the middle of the white region.

Then, I'd like to obtain a result like this:

enter image description here

HOWEVER

I am using the following command in ImageMagick 6.8.9-9 Q16 x86_64 2017-07-31 in Ubuntu 16.04:

convert \( Image1.png -resize 447x640 \) \( -compose Overlay Image2.png \) -gravity north -composite Image3.png

I've tried countless times but the best result I can get (by using command above) is Image 3. Can anyone help me? Thank you.

Image 3

enter image description here


Solution

  • I think this is what you want using Imagemagick in Unix syntax:

    Img:

    enter image description here

    Mask:

    enter image description here

    convert \( mask.png -alpha off \) img.jpg \( mask.png -alpha extract -negate \) -compose over -composite result.png
    


    or more simply:

    convert mask.png img.jpg -compose dstover -composite result.png
    

    enter image description here