Page 1 of 1

Preserving Alpha with MagickCompositeImage

Posted: 2013-04-13T09:05:18-07:00
by PeterUK
I'm using MagickCompositeImage to read in a "border" image which has some partial transparency in it, but when I composite it onto my background, the partial transparency is turned to black, but the fully transparent parts are still transparent.

Any tips on what I'm doing wrong?

Code: Select all

// Read in images
$image_body = NewMagickWand();
$image_header = NewMagickWand();
$border_img = NewMagickWand();
MagickReadImage($image_body, "body.png");
MagickReadImage($image_header, "header.png");
MagickReadImage($border_img, "border.png");

// Create main canvas
$main_image = NewMagickWand();
MagickSetFormat($main_image, 'PNG24');
$transparent = NewPixelWand();
PixelSetColor($transparent, 'none');
MagickNewImage($main_image, 350, 150, $transparent);
MagickCompositeImage($main_image, $image_body, MW_OverCompositeOp, 2, 26);
MagickCompositeImage($main_image, $image_header, MW_OverCompositeOp, 2, 2);
MagickCompositeImage($main_image, $border_img, MW_OverCompositeOp, 0, 0);

header("Content-Type: image/png");
MagickEchoImageBlob($main_image);
I tried using MW_CopyOpacityCompositeOp instead of MW_OverCompositeOp when copying the border but then it just seems to copy nothing at all.

Re: Preserving Alpha with MagickCompositeImage

Posted: 2013-04-13T20:49:06-07:00
by el_supremo
Try enabling the alpha channel in the main image after creating it:

Code: Select all

MagickSetImageAlphaChannel($main_image,MW_SetAlphaChannel);
Pete

Re: Preserving Alpha with MagickCompositeImage

Posted: 2013-04-14T05:23:10-07:00
by PeterUK
Thanks, I already tried that and I tried setting it to the border image as well.

I also tried this:

Code: Select all

MagickSetImageAlphaChannel($main_image, MW_ActivateAlphaChannel);
MagickSetImageAlphaChannel($border_img, MW_ActivateAlphaChannel);
For both and that didn't work either.

Re: Preserving Alpha with MagickCompositeImage

Posted: 2013-04-15T16:49:26-07:00
by anthony
You are writing to a PNG24 format... that format has no transparency. so any transparency left after the
compositions will be lost (making the hidden fully-transparent color visible).

Try using PNG32

PS; I would move the 'SetFormat' to just before you write as that is where it is needed. Yes it can be set before but that disconnects the relevant setting from the operator.

Re: Preserving Alpha with MagickCompositeImage

Posted: 2013-04-16T03:39:48-07:00
by PeterUK
anthony wrote:You are writing to a PNG24 format... that format has no transparency. so any transparency left after the
compositions will be lost (making the hidden fully-transparent color visible).

Try using PNG32

PS; I would move the 'SetFormat' to just before you write as that is where it is needed. Yes it can be set before but that disconnects the relevant setting from the operator.
Thank you, that sorted it out!

Re: Preserving Alpha with MagickCompositeImage

Posted: 2015-03-11T03:56:09-07:00
by ralphysunsmith
Thank you for giving this suggestion, I have encountered this same format before and I was totally lost how to figure it out, It has given me an idea on how to work on it.