Page 1 of 1
[SOLVED] How to remove image background by using mask?
Posted: 2014-02-06T11:28:12-07:00
by RyanBram
Hi.
I need some help for removing image background using another image as mask. The image is as following:
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.
Re: How to remove image background by using mask?
Posted: 2014-02-06T11:36:02-07:00
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.
Re: How to remove image background by using mask?
Posted: 2014-02-06T11:54:50-07:00
by RyanBram
Many thanks for fast reply.
It works like charm.
Is it also possible for ImageMagick to create transparent and semi transparent image with these two pictures:
By the way, I cannot find any way to spoil those images, which make this thread a little bit messy. Sorry.
Regards.
Re: How to remove image background by using mask?
Posted: 2014-02-06T12:32:16-07:00
by fmw42
Not sure what you want?
But see background removal at
http://www.imagemagick.org/Usage/masking/#bg_remove
Re: How to remove image background by using mask?
Posted: 2014-02-06T12:37:51-07:00
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
Re: How to remove image background by using mask?
Posted: 2014-02-06T13:15:35-07:00
by RyanBram
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:

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.

Re: How to remove image background by using mask?
Posted: 2014-02-06T13:22:02-07:00
by snibgo
That's called "anti-aliasing" or "feathering". See further down the link fmw gave.
Re: How to remove image background by using mask?
Posted: 2014-02-06T13:52:09-07:00
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.
Re: How to remove image background by using mask?
Posted: 2014-02-06T14:17:34-07:00
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.
Re: How to remove image background by using mask?
Posted: 2014-02-06T15:33:25-07:00
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
Re: How to remove image background by using mask?
Posted: 2014-02-07T04:17:40-07:00
by RyanBram
Hooray, amazing. Thanks.
Problem Solved.