[SOLVED] Automatically Remove Background Color

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] Automatically Remove Background Color

Post by RyanBram » 2014-02-10T12:00:36-07:00

Hi.
I want to remove background color from my images. The sample of images is as follow
Image

I tried following command:

Code: Select all

convert Tree.png -fill none -draw "matte 0,0 floodfill" TransparentTree.png
and this is the result that I got.
Image

How to make all of black color become transparent instead of just an area of black.

Regards.
Last edited by RyanBram on 2014-02-12T08:12:10-07:00, edited 1 time 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: 11809
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Automatically Remove Background Color

Post by snibgo » 2014-02-10T13:57:41-07:00

Your image has gray pixels around the edge and at 0,0. Perhaps it has been mangled by the image hosting site.

Your second image shows correct working of floodfill. It has stopped at non-black pixels. You could add a black border first, floodfill from 0,0, and finally shave off the border.

The easier way of making all black pixels transparent is:

Code: Select all

convert -transparent black out.png
snibgo's IM pages: im.snibgo.com

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

Re: Automatically Remove Background Color

Post by fmw42 » 2014-02-10T13:58:18-07:00

try

convert Tree.png.jpeg -fuzz 10% -transparent black result.png

This looks for any pixel within 10% of pure black and makes it transparent.

see
http://www.imagemagick.org/Usage/color_basics/#opaque

P.S. In windows syntax you need to use %% rather than %

see
http://www.imagemagick.org/Usage/windows/

RyanBram
Posts: 30
Joined: 2013-03-25T22:55:10-07:00
Authentication code: 6789

Re: Automatically Remove Background Color

Post by RyanBram » 2014-02-10T14:17:03-07:00

snibgo wrote:Your image has gray pixels around the edge and at 0,0. Perhaps it has been mangled by the image hosting site.
Do you mean the thumbnail? I clicked the image to link to actual image and it seems okay.
Even I suggested the usage of the hosting for this forum.

Sorry if I cannot give good explanation, but what I want to achieve is for using ImageMagick to remove all background by using top-left pixel as sample. Something like
convert tree.png -alpha set -transparent #000000 TransparentTree.png
, but using top-left pixel as sample instead of #000000.
Because I have a lot of images which have non black background colors. So, whatever it is black, purple, blue, or etc, ImageMagick just look for top-left pixel as sample.

Thanks.
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: 11809
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Automatically Remove Background Color

Post by snibgo » 2014-02-10T15:00:03-07:00

"-transparent" needs an actual colour. It would be nice if it accepted syntax like ...

Code: Select all

convert tree.png.jpg -transparent "%%[pixel:{0,0}]" t.png
... but it doesn't. So you have to extract the colour with a separate command. This depends on your scripting language. For example, in Windows BAT:

Code: Select all

FOR /F "usebackq" %%C ^
IN (`convert tree.png -format "%%[pixel:p{0,0}]" info:`) ^
DO convert tree.png -transparent %%C out.png
snibgo's IM pages: im.snibgo.com

RyanBram
Posts: 30
Joined: 2013-03-25T22:55:10-07:00
Authentication code: 6789

Re: Automatically Remove Background Color

Post by RyanBram » 2014-02-11T01:36:01-07:00

snibgo wrote:"-transparent" needs an actual colour. It would be nice if it accepted syntax like ...
I am very agree to you. Is it possible for me to suggest such feature to ImageMagick Developers? How could I do that?
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: 11809
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Automatically Remove Background Color

Post by snibgo » 2014-02-11T05:58:46-07:00

You can write a post in the developers forum: viewforum.php?f=2
snibgo's IM pages: im.snibgo.com

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

Re: Automatically Remove Background Color

Post by fmw42 » 2014-02-11T15:44:46-07:00

I believe that this is what you want to do using replace rather than floodfill:
convert Tree.png.jpeg -fuzz 10% -fill none -draw "matte 0,0 replace" result.png
see section on matte x,y method
http://www.imagemagick.org/script/magic ... aphics.php


In Windows syntax, you may need to use %% rather than %.

RyanBram
Posts: 30
Joined: 2013-03-25T22:55:10-07:00
Authentication code: 6789

Re: Automatically Remove Background Color

Post by RyanBram » 2014-02-12T08:10:59-07:00

snibgo wrote:

Code: Select all

FOR /F "usebackq" %%C ^
IN (`convert tree.png -format "%%[pixel:p{0,0}]" info:`) ^
DO convert tree.png -transparent %%C out.png
fmw42 wrote:

Code: Select all

convert Tree.png.jpeg -fuzz 10% -fill none -draw "matte 0,0 replace" result.png
Result:
Image

Both worked. Thanks for all.
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