[SOLVED] How to remove image background by 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
RyanBram
Posts: 30
Joined: 2013-03-25T22:55:10-07:00
Authentication code: 6789

[SOLVED] How to remove image background by using mask?

Post by RyanBram »

Hi.
I need some help for removing image background using another image as mask. The image is as following:
Image
Image

The result that I prefer is the image which also has semi transparent area in each edge, so it will look nice to be placed in many background colors.


Regards.
Last edited by RyanBram on 2019-06-17T10:42:51-07:00, edited 3 times in total.
OS: Windows 7 Ultimate, 32-Bit
CPU: Intel Core i3 2350M @ 2.30GHz
RAM: 2.00 GB Single-Channel DDR3 @ 665MHz
IM Version: 6.8.3-8 2013-03-04 Q16
Features: DPC OpenMP
Delegates: bzlib fontconfig freetype jng jp2 jpeg lcms lzma pango png ps tiff x xml zlib
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to remove image background by using mask?

Post by fmw42 »

try

convert image \( mask -negate \) -alpha off -compose copy_opacity -composite result.png

if on windows use (...) rather than \(...\) and note there must be spaces on each side of the ( and ).

The -negate is because the mask needs to be white where you want to keep the other image unchanged and black where you want the other image to be transparent. You need to save to PNG or TIFF to maintain transparency. JPG does not support transparency.
RyanBram
Posts: 30
Joined: 2013-03-25T22:55:10-07:00
Authentication code: 6789

Re: How to remove image background by using mask?

Post by RyanBram »

Many thanks for fast reply.
It works like charm.
Image

Is it also possible for ImageMagick to create transparent and semi transparent image with these two pictures:
Image
Image

By the way, I cannot find any way to spoil those images, which make this thread a little bit messy. Sorry.

Regards.
Last edited by RyanBram on 2019-06-17T10:43:29-07:00, edited 2 times in total.
OS: Windows 7 Ultimate, 32-Bit
CPU: Intel Core i3 2350M @ 2.30GHz
RAM: 2.00 GB Single-Channel DDR3 @ 665MHz
IM Version: 6.8.3-8 2013-03-04 Q16
Features: DPC OpenMP
Delegates: bzlib fontconfig freetype jng jp2 jpeg lcms lzma pango png ps tiff x xml zlib
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to remove image background by using mask?

Post by fmw42 »

Not sure what you want?

But see background removal at http://www.imagemagick.org/Usage/masking/#bg_remove
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to remove image background by using mask?

Post by snibgo »

You can make a mask, eg:

Code: Select all

convert screenshot_1.png screenshot_2.png -compose Difference -composite -fill white +opaque black m.png
and then do whatever you want, eg turn the man into a ghost:

Code: Select all

convert screenshot_1.png screenshot_2.png ( m.png -evaluate Multiply 0.7 ) -compose over -composite f.png
snibgo's IM pages: im.snibgo.com
RyanBram
Posts: 30
Joined: 2013-03-25T22:55:10-07:00
Authentication code: 6789

Re: How to remove image background by using mask?

Post by RyanBram »

fmw42 wrote:Not sure what you want?

But see background removal at http://www.imagemagick.org/Usage/masking/#bg_remove
I want to remove the background, so only the character left.
snibgo wrote:You can make a mask, eg:

Code: Select all

convert screenshot_1.png screenshot_2.png -compose Difference -composite -fill white +opaque black m.png
and then do whatever you want, eg turn the man into a ghost:

Code: Select all

convert screenshot_1.png screenshot_2.png ( m.png -evaluate Multiply 0.7 ) -compose over -composite f.png
Thanks snibgo for the tips. By combining your technique, with one from Fred, the result is this:
Image
Not as smooth as my previous result, but still acceptable.

So there isn't a technique to make the bleeding edge to become semi-transparent with ImageMagick, is it?

Cheers. :)
Last edited by RyanBram on 2019-06-17T10:44:05-07:00, edited 2 times in total.
OS: Windows 7 Ultimate, 32-Bit
CPU: Intel Core i3 2350M @ 2.30GHz
RAM: 2.00 GB Single-Channel DDR3 @ 665MHz
IM Version: 6.8.3-8 2013-03-04 Q16
Features: DPC OpenMP
Delegates: bzlib fontconfig freetype jng jp2 jpeg lcms lzma pango png ps tiff x xml zlib
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to remove image background by using mask?

Post by snibgo »

That's called "anti-aliasing" or "feathering". See further down the link fmw gave.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to remove image background by using mask?

Post by fmw42 »

try


convert screenshot_1.png screenshot_2.png \
\( -clone 0 -clone 1 -compose Difference -composite -fill white +opaque black -blur 0x1 \) \
-delete 1 -alpha off -compose copy_opacity -composite screenshot_1_masked.png

or on windows


convert screenshot_1.png screenshot_2.png ^
( -clone 0 -clone 1 -compose Difference -composite -fill white +opaque black -blur 0x1 ) ^
-delete 1 -alpha off -compose copy_opacity -composite screenshot_1_masked.png

Increase or decrease the blur value from 1 as desired.
RyanBram
Posts: 30
Joined: 2013-03-25T22:55:10-07:00
Authentication code: 6789

Re: How to remove image background by using mask?

Post by RyanBram »

fmw42 wrote:Increase or decrease the blur value from 1 as desired.
Did you mean -blur 0x1, -blur 0x2, -blur 0x3, ...
or -blur 1x1, -blur 2x1, -blur 3x1, ...
or both.

Sorry for any inconvenience.
OS: Windows 7 Ultimate, 32-Bit
CPU: Intel Core i3 2350M @ 2.30GHz
RAM: 2.00 GB Single-Channel DDR3 @ 665MHz
IM Version: 6.8.3-8 2013-03-04 Q16
Features: DPC OpenMP
Delegates: bzlib fontconfig freetype jng jp2 jpeg lcms lzma pango png ps tiff x xml zlib
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to remove image background by using mask?

Post by fmw42 »

-blur radiusxsigma where radius=0 and sigma is varied. Using radius 0 tells IM to calculate it automatically. Sigma can be fractional (down to about 0.5). See http://www.imagemagick.org/script/comma ... s.php#blur
RyanBram
Posts: 30
Joined: 2013-03-25T22:55:10-07:00
Authentication code: 6789

Re: How to remove image background by using mask?

Post by RyanBram »

Hooray, amazing. Thanks.
Problem Solved.
OS: Windows 7 Ultimate, 32-Bit
CPU: Intel Core i3 2350M @ 2.30GHz
RAM: 2.00 GB Single-Channel DDR3 @ 665MHz
IM Version: 6.8.3-8 2013-03-04 Q16
Features: DPC OpenMP
Delegates: bzlib fontconfig freetype jng jp2 jpeg lcms lzma pango png ps tiff x xml zlib
Post Reply