Crop image using a black/white mask

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
canduc17
Posts: 1
Joined: 2011-04-28T01:47:54-07:00
Authentication code: 8675308

Crop image using a black/white mask

Post by canduc17 »

Hi everyone.

I have 2 png images of the same size and one of them is a mask: a white circle in the center with every other pixels black.
I would like to obtain a cropped image with the original one substituting the white circle and with the other pixels black.

I've tried with this tutorial, typing

Code: Select all

convert mask.png orig.png -compose ChangeMask  -composite  cropped.png
but I've obtained an image equal to the mask.

Have you got any suggestions?

Thanks in advance!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Crop image using a black/white mask

Post by fmw42 »

try either of these (assuming your mask is a white circle surrounded by black)

convert mask.png image.png mask.png -compose multiply -composite result.png

convert image.png mask.png -compose copy_opacity -composite -compose over -background black -flatten result.png
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Crop image using a black/white mask

Post by anthony »

See IM Examples, Masking, Mathematical and Alpha Composition
http://www.imagemagick.org/Usage/masking/#compose
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply