Page 1 of 1

[SOLVED] Automatically Remove Background Color

Posted: 2014-02-10T12:00:36-07:00
by RyanBram
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.

Re: Automatically Remove Background Color

Posted: 2014-02-10T13:57:41-07:00
by snibgo
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

Re: Automatically Remove Background Color

Posted: 2014-02-10T13:58:18-07:00
by fmw42
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/

Re: Automatically Remove Background Color

Posted: 2014-02-10T14:17:03-07:00
by RyanBram
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.

Re: Automatically Remove Background Color

Posted: 2014-02-10T15:00:03-07:00
by snibgo
"-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

Re: Automatically Remove Background Color

Posted: 2014-02-11T01:36:01-07:00
by RyanBram
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?

Re: Automatically Remove Background Color

Posted: 2014-02-11T05:58:46-07:00
by snibgo
You can write a post in the developers forum: viewforum.php?f=2

Re: Automatically Remove Background Color

Posted: 2014-02-11T15:44:46-07:00
by fmw42
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 %.

Re: Automatically Remove Background Color

Posted: 2014-02-12T08:10:59-07:00
by RyanBram
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.