title: Mask PNG image with black and white mask
body: <p>I have the following image (please note the transparent background):</p>
  
  <p><a href="https://i.stack.imgur.com/5DYL2.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5DYL2.png" alt="enter image description here"></a></p>
  
  <p>I also have a black/white mask <strong>of the same size</strong>:</p>
  
  <p><a href="https://i.stack.imgur.com/mlDws.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mlDws.png" alt="enter image description here"></a></p>
  
  <p>I would like to "crop" the dress and get just the portion of the first image contained in the black circle. I tried many different methods but they didn't work or are too slow:</p>
  
  <p>1) ImageMagick (command line) &lt;== which command can I use to achieve this? I tried multiply and copyopacity but they didn't work</p>
  
  <p>2) WideImage is working: <code>$maskedImage = $source-&gt;applyMask($mask);</code> but it takes more than 12 seconds.</p>
  
  <p>I am interested in a ImageMagick solution if possible.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>The provided solutions work fine if the mask is smaller than the original image and if the original image is simple. With these source image and mask the result is "smeared":</p>
  
  <p>Source:</p>
  
  <p><a href="https://i.stack.imgur.com/zXhKY.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zXhKY.png" alt="enter image description here"></a></p>
  
  <p>Mask:</p>
  
  <p><a href="https://i.stack.imgur.com/kCKco.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/kCKco.png" alt="enter image description here"></a></p>
  
  <p>Command:</p>
  
  <p><code>convert source.png \( mask.png -negate \) -alpha off -compose copy_opacity -composite result.png</code></p>
  
  <p>Result (I added a grey background instead of the transparent one in order to show the wrong white):</p>
  
  <p><a href="https://i.stack.imgur.com/ndIUk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ndIUk.png" alt="enter image description here"></a></p>