NOTE: the following is based on the original question. Involving images contains transparency. If that is not the case, then greysacle mask generation is needed, as shown by fwm42. If the images involved actually has existing transparency then read on....
Copying the transparency from one image to another image is easy.
That is the original purpose of the compose method
CopyOpacity
You just have to make sure the source image as a alpha channel, as if it does not it will think
the source image is a greyscale mask of the alpha transparency.
Code: Select all
convert image_to_get_transparency image_with_transparency \
-compose CopyOpacity -composite new_image
See opening notes of
http://www.imagemagick.org/Usage/compose/#copyopacity for more details.
HOWEVER!!! I do not recommend it, as if the first image already contains alpha, the "CopyOpacity" method, OR even just extracting a greyscale mask and using that, can result in some transparent pixels in the 'destination' image becoming opaque. These pixels has undefined color (they were fully-transparent) and so you could end up with a horrible mess...
The better way is to use "Dst_In" or Dst_Atop composition methods.
http://www.imagemagick.org/Usage/compose/#dstin
these methods ensure that at no time does a fully transparent pixel suddenly becomes opaque.
Not only that you do
not need to extract a intermediate grayscale mask, but can use the transparency of the source image directly,
These composition methods are known as
Alpha Composition is because they are designed to mix images based on the alpha transparency of an image. They are the better way of handling transparency. Greyscale masks should only be used when the operation becomes so complex that you need much lower-level image processing to perform the desired task.