Page 1 of 1

How to Combine Image Channels

Posted: 2016-01-18T11:57:57-07:00
by cthomas
Hey, I had something of an issues with the API's annotate method, with a black border appearing around its text, when it was composited over another images. As a result, I decided to create my own image, using the text as an alpha channel. This was a real pain, as how to combine channels in this API (lordy knows I searched the internet...). So here was the plan.

* Create a black opaque image
* Write the text onto it in white.

This gives us our alpha channel. Next...

* Create an opaque white image
* Use this white image as the R,G and B of a new combined image. And use the Black/White text image as its alpha.

Here's how I did this.

Code: Select all

  Magick::Image txtImage;
  txtImage.magick("RGBA");

  Magick::Image whiteImage;
  whiteImage.magick("RGBA");

  std::vector<Magick::Image> imagesList; 	//  The list of images that will form the new combined image
  imagesList.push_back(whiteImage.separate(Magick::RedChannel)); 	// Will be used for R
  imagesList.push_back(whiteImage.separate(Magick::GreenChannel)); 	// Will be used for G
  imagesList.push_back(whiteImage.separate(Magick::BlueChannel)); 	// Will be used for B
  imagesList.push_back(txtImage); 		// Will be used for Alpha

  Magick::Image combinedImages;
  combinedImages.magick("RGBA");

  Magick::combineImages( &combinedImages, imagesList.begin(), imagesList.end( ), Magick::AllChannels );
later I discovered that if you do not supply each channel of the image to be composited, separately, the combineImages method, uses (guessing) the red channel from each image (it could be the luma average of all?). So in the example above, in the imagesList.push, you'll see .separate() being used to do this...

Hope this is of help to someone

Re: How to Combine Image Channels

Posted: 2016-01-18T13:18:15-07:00
by fmw42
In command line, you would use -compose copy_opacity -composite to put an image into the alpha channel. Sorry, I do not know how to do that in your API. But I would suspect it is done through the use of the composite equivalent in your API.

Re: How to Combine Image Channels

Posted: 2016-01-20T08:38:17-07:00
by cthomas
"In command line, you would use -compose copy_opacity -composite to put an image into the alpha channel. Sorry, I do not know how to do that in your API. But I would suspect it is done through the use of the composite equivalent in your API."

Please see above, it shows how to do the operation in the c++ API.

To be clear, this is combining the RGB of image A, with image B supplying the Alpha channel, to produce a new image C.