[SOLVED] How to remove image background by using mask?
[SOLVED] How to remove image background by using mask?
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.
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.
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
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
- 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?
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.
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?
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.
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.
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
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
- 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?
Not sure what you want?
But see background removal at http://www.imagemagick.org/Usage/masking/#bg_remove
But see background removal at http://www.imagemagick.org/Usage/masking/#bg_remove
-
- 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?
You can make a mask, eg:
and then do whatever you want, eg turn the man into a ghost:
Code: Select all
convert screenshot_1.png screenshot_2.png -compose Difference -composite -fill white +opaque black m.png
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
Re: How to remove image background by using mask?
I want to remove the background, so only the character left.fmw42 wrote:Not sure what you want?
But see background removal at http://www.imagemagick.org/Usage/masking/#bg_remove
Thanks snibgo for the tips. By combining your technique, with one from Fred, the result is this:snibgo wrote:You can make a mask, eg:and then do whatever you want, eg turn the man into a ghost:Code: Select all
convert screenshot_1.png screenshot_2.png -compose Difference -composite -fill white +opaque black m.png
Code: Select all
convert screenshot_1.png screenshot_2.png ( m.png -evaluate Multiply 0.7 ) -compose over -composite f.png
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
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
-
- 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?
That's called "anti-aliasing" or "feathering". See further down the link fmw gave.
snibgo's IM pages: im.snibgo.com
- 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?
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.
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?
Did you mean -blur 0x1, -blur 0x2, -blur 0x3, ...fmw42 wrote:Increase or decrease the blur value from 1 as desired.
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
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
- 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?
-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?
Hooray, amazing. Thanks.
Problem Solved.
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
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