Page 1 of 1

Add an existing alpha data from image to another image

Posted: 2010-11-23T10:52:02-07:00
by lilith2
Hello everybody, thanks in advance for your time.

I want to add data to an image alpha channel from an existing image.

Example : I want to add to IMAGE.PNG (in alpha channel data), from ALPHA.PNG.

Thanks for your help.

Re: Add an existing alpha data from image to another image

Posted: 2010-11-23T11:08:49-07:00
by fmw42
Do you just want to add the alpha from the one image to the other that does not have an alpha or do you need to merge the two alphas?

If the former:

convert image.png \( alpha.png -extract \) \ -alpha off -compose copy_opacity -composite image_with_alpha.png

see

http://www.imagemagick.org/Usage/basics/#parenthesis
http://www.imagemagick.org/Usage/basics/#alpha

Note the above is for Unix/Mac. For windows differences see
http://www.imagemagick.org/Usage/windows/

In windows you don't need the \ before parentheses

Re: Add an existing alpha data from image to another image

Posted: 2010-11-23T11:15:57-07:00
by lilith2
Thanks for your reply.

I want to add the alpha from one image to an other which have an alpha channel with no data.

To be clear, i want to add this :
http://www.glowfoto.com/viewimage.php?i ... 0&srv=img4

To this :
http://www.glowfoto.com/viewimage.php?i ... 0&srv=img5

Thanks again.

Re: Add an existing alpha data from image to another image

Posted: 2010-11-23T11:26:57-07:00
by fmw42
If you want to use one grayscale image as transparency over another color image that does not have transparency, then try

convert colorimage grayscaleimage -alpha off -alpha copy result.png

or if that does not work, then

convert colorimage grayscaleimage -alpha off -alpha copy -type truecolormatte PNG32:result.png

Note both images need to be the same size.

Re: Add an existing alpha data from image to another image

Posted: 2010-11-23T11:33:33-07:00
by lilith2
Both commands did not work.
It gives me two different files:

http://www.glowfoto.com/viewimage.php?i ... 0&srv=img4
http://www.glowfoto.com/viewimage.php?i ... 0&srv=img6

And what i want is :
http://www.glowfoto.com/viewimage.php?i ... 0&srv=img6

I can do it with PixelFormer, but i try to do it in batch :)

Thanks.

EDIT : correct links to images.

Re: Add an existing alpha data from image to another image

Posted: 2010-11-23T11:38:15-07:00
by GreenKoopa
You give the impression that at least one of your images has an alpha channel, but I think you mean that you would like a grayscale image to be used AS an alpha channel.

Code: Select all

convert image.png mask.png -alpha off -compose copy_opacity -composite PNG32:result.png
Image + Image = Image

http://www.imagemagick.org/Usage/compose/#copyopacity

Re: Add an existing alpha data from image to another image

Posted: 2010-11-23T11:42:33-07:00
by lilith2
GreenKoopa, fmw42,

It's exactly what i want. Thanks a lot.

Greetings.

Re: Add an existing alpha data from image to another image

Posted: 2010-11-23T16:51:48-07:00
by anthony
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.