Reputation: 1767
Imagine I have rgb.jpg and alpha.png. How to combine them into rgba.png with Imagemagick?
Upvotes: 8
Views: 5366
Reputation: 94804
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
Upvotes: 21