Remove png image using 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
filipemansano
Posts: 9
Joined: 2017-06-07T12:24:32-07:00
Authentication code: 1151

Remove png image using mask

Post by filipemansano »

Hi, i need remove part of image using mask (black and white)
my original image is a PNG image no-background, using mask i need to keep white area and remove black area

i try

Code: Select all

convert original.png mascara.png -alpha off -compose CopyOpacity -composite out.png
this command remove using mask ,But the end result is coming up with a black background

Original image: https://ibb.co/kFZwoQ
Mask image: https://ibb.co/dtuCTQ
Result: https://ibb.co/dGfxv5

How do I remove using the mask, but keeping the background transparent?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Remove png image using mask

Post by fmw42 »

Your original.png image has transparency. So you need to combine the transparency of the original.png and mascara.png. You have not said what version of IM you have nor your platform. Please always provide that when asking questions.

In Unix:

Code: Select all

convert original.png mascara.png \
\( -clone 0 -alpha extract \) \
\( -clone 1,2 -compose multiply -composite \) \
-delete 1,2 -alpha off -compose over -compose copy_opacity -composite result.png
In Windows:

Code: Select all

convert original.png mascara.png ^
( -clone 0 -alpha extract ) ^
( -clone 1,2 -compose multiply -composite ) ^
-delete 1,2 -alpha off -compose over -compose copy_opacity -composite result.png
I have split the command into multiple lines, so if you copy and past, be sure that there are no spaces after the ending characters / or ^, depending upon your OS.

For one long line of code:

In Unix:

Code: Select all

convert original.png mascara.png \( -clone 0 -alpha extract \) \( -clone 1,2 -compose multiply -composite \) -delete 1,2 -alpha off -compose over -compose copy_opacity -composite result.png
In Windows:

Code: Select all

convert original.png mascara.png ( -clone 0 -alpha extract ) ( -clone 1,2 -compose multiply -composite ) -delete 1,2 -alpha off -compose over -compose copy_opacity -composite result.png
filipemansano
Posts: 9
Joined: 2017-06-07T12:24:32-07:00
Authentication code: 1151

Re: Remove png image using mask

Post by filipemansano »

worked perfectly!
Sorry, I really forgot to tell the version.

SO: Windows
Version: ImageMagick 7.0.6-0 Q16 x64 2017-06-11

thank you again
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Remove png image using mask

Post by fmw42 »

For IM 7, change convert to magick
Post Reply