How to combine RGB image and 8-bit alpha image into single PNG file with Imagemagick?
Imagine I have rgb.jpg and alpha.png. How to combine them into rgba.png with Ima开发者_如何学JAVAgemagick?
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
精彩评论