Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to combine RGB image and 8-bit alpha image into single PNG file with Imagemagick?

Tags:

imagemagick

Imagine I have rgb.jpg and alpha.png. How to combine them into rgba.png with Imagemagick?

like image 670
Alexander K. Avatar asked Apr 13 '11 10:04

Alexander K.


1 Answers

Since you didn't specify, I'll assume a sufficiently new version of ImageMagick being run from the command line. This was tested using ImageMagick 6.6.9-7 on Linux (from Debian).

If alpha.png is an image from which you want to copy the alpha channel, or it is a greyscale image with no alpha channel, you can use the following:

convert rgb.jpg alpha.png -compose copy-opacity -composite rgba.png

Otherwise, you will have to do a bit more processing on alpha.png. For example, this converts alpha.png to greyscale and ignores the alpha channel:

convert rgb.jpg ( alpha.png -colorspace gray -alpha off ) -compose copy-opacity -composite rgba.png

Note that you may have to escape the parentheses in the latter command, depending on your command-line environment.

If necessary, you can also invert the greyscale image before using it as the alpha channel:

convert rgb.jpg ( alpha.png -colorspace gray -alpha off -negate ) -compose copy-opacity -composite rgba.png
like image 108
Anomie Avatar answered Sep 21 '22 14:09

Anomie