How to add an alpha channel to an RGB image?

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?".
Post Reply
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Post by anthony »

The +matte in this case ensures that the 'CopyOpacity' will deal with a grey scale image. In the grey.png image has a matte channel than it copys the matte channel rather than using the grey colors of the image. It is a historical quirk of the operator.

For more details see CopyOpacity Alpha Composition
http://www.cit.gu.edu.au/~anthony/graph ... opyopacity

A matte channel is always added if not already present to the destination image.

NOTE this command can make parts of the destination image that was transparent, opaque. This is usally not a good thing as the color of transparent areas are basically undefined, and usally (due to mathematical handling) is black. This often caused all sorts of problems in IM version 5 which were slowly resolved in the earlier versions of IM v6.

See the various transparency bugs of old versions which are docuimented in IM Examples, Bugs and Development
http://www.cit.gu.edu.au/~anthony/graph ... ick6/bugs/

The best solution to this is to only used CopyOpacity using two images that do not have any transparency to begin with, as such +matte sort of ensures that is the case.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Post by anthony »

Now you really have a problem!!!!

If you just try to use multiple files, Im can't figure out what pair of images need to be composted together. In other words it does NOT work, as Im can not tell what to do.

The solution is to use mogrify, but in a very specal way. You need to do the alpha compositing using -draw which can separateally define the image to overlay outside the normal image sequence.

Code: Select all

   mogrify  -draw 'image {compose} 0,0 "source_image"'  *.jpg
Note however that the above does not, and can not remove the transparency from the overlay image. As such 'CopyOpacity' will only work if you can be sure the saved source mask has no matte channel when it was saved.

Because of this you are better off using a proper 'shaped' image as the mask.
here is one way to convert a greyscale mask into a shaped image, with the mask as the images 'alpha channel'.

Code: Select all

   convert grey.pgm +matte -channel A -combine +channel  shape.png
Now you can use 'Dst_In' to find the parts of the destanation (or background) image that falls 'IN' the shaped image...

Code: Select all

   mogrify -format png  -matte  -draw 'image Dst_In 0,0 "shape.png"'  *.jpg
If speed is important and images are large you can speed things up by using the 'MPC' format fo rthe shape image, as IM will be reading this image a lot! See the IM Archeture document for more info.

Note the use of PNG format to preserve semi-transparent pixels. You can NOT use JPG and transparency!!!! I also had to make sure a alpha channel was added!

I have updated the IM examples "mogrify" section as this is a better simpler example than the complex one I previously had...
http://www.cit.gu.edu.au/~anthony/graph ... fy_compose
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Post by anthony »

Re-reading your original problem specification, mogrify will not work for you, as you have a different alpah 'mask' for each image.

Currently you will have to loop though all the image JPG images and add the approraite alpha mask, one image at a time.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Post by anthony »

Examples of non-mogrify image loops are provided in
http://www.cit.gu.edu.au/~anthony/graph ... ogrify_not

However these are unix/linux/citrix centric. I suggest you install citrix to get these scripting features.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply