Search code examples
imagemagicktransparency

Clipping image to transparent mask


I have these images with associated masks that have transparency, and I want to apply the alpha of the mask onto the original image. For instance: Image - https://i.imgur.com/8Nb4VJ8.png Mask - https://i.imgur.com/anflRUr.png

I've gone through the documentation and loads of threads but nothing has worked for me. Most people seem to have black and white mask images and they want to use the brightness as the mask, but in my case I want to simply copy the alpha channel of the mask. I imagine I'm missing some simple option, but I haven't been able to figure it out. I would appreciate some assistance.

Thank you.


Solution

  • I am not positive I understand what you want. But if you want to apply the mask to the alpha channel of the image, then this should do what you want. You need to extract the alpha channel of the mask and apply that to the image as follows:

    Image:

    enter image description here

    Mask:

    enter image description here

    Unix Syntax for Imagemagick 6:

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

    enter image description here

    If using Windows, remove the \ from the parentheses. If using Imagemagick 7, change convert to magick