Add an existing alpha data from image to another 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
lilith2

Add an existing alpha data from image to another image

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post 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
lilith2

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

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post 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.
lilith2

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

Post 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.
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

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

Post 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
lilith2

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

Post by lilith2 »

GreenKoopa, fmw42,

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

Greetings.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

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

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply