Basic mask question

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
jasonim
Posts: 1
Joined: 2012-07-31T16:16:49-07:00
Authentication code: 15

Basic mask question

Post by jasonim »

This is a newbie question. I searched first as much I could, but I am probably not getting the logic of ImageMagick yet...

I have two images, a RGB PNG file and a PNG with alpha mask, same size. I want to completely black out the parts of the first image not present in the mask. What's the most efficient way to do this? Do I need to first convert the PNG with alpha mask to one that is grayscale but doesn't have an alpha channel?

I tried things like

Convert <INPUTFILE> -mask <MASKFILE> -channel RGB -evaluate set 0 +channel <OUTPUTFILE>

or

Convert <INPUTFILE> -mask <MASKFILE> -fill black -draw "rectangle 0,0 599,599" <OUTPUTFILE>

These almost work, but the unprotected area merely gets much darker, doesn't turn black.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Basic mask question

Post by fmw42 »

It is not clear what you are trying to do. Is the mask in the second image binary? As a guess, assuming it is, try this

convert firstimage \( secondimage -alpha extract \) -compose multiply -composite resultimage

If the alpha channel is binary then it will be white where you want to keep the first image and black where you want the first image blackened. So in this case white=1 and black=0. So a compose multiply will do what I think you want.

see
http://www.imagemagick.org/Usage/compose/
http://www.imagemagick.org/Usage/basics/#parenthesis
http://www.imagemagick.org/Usage/masking/#alpha


I presume both images are the same size.


If this does not do what you want, then post links to your images, esp the second with the alpha channel. You can find many free image hosting sites to upload your images and then link them from there to here in your posts.

Also in future, please post your operating system and version of IM.

P.S. The above command in in unix syntax. If you are on windows, remove the \ for before the parens. See http://www.imagemagick.org/Usage/windows/ for syntax differences.
Post Reply