cimagemagicktransparentcompositemagickwand

ImageMagick composite two images and fill transparent data with original data


I have two 256X256 images, one has full purple background and the other has some of it transparent.

I want to copy the second image into the first image, so that the transparent data from the second image is filled by the data from the first image, resulting this:

My attempt of doing so ended with the entire first image replaced by the second image. I used the OverCompositeOp operator:
MagickCompositeImage(wand1, wand2, OverCompositeOp, MagickFalse, 0, 0);


Solution

  • You probably have the two images swapped. You need to overlay the transparent one on top of the opaque one. This assumes they both are the same size. In ImageMagick 7 command line, this works fine.

    magick purple.png blue_transparent.png -compose over -composite result.png
    

    enter image description here

    For ImageMagick 6, replace magick with convert