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
lsintampa
Posts: 3
Joined: 2013-07-12T05:57:58-07:00
Authentication code: 6789

Remove background color????

Post by lsintampa »

Do you know if it is possible for imagemagick to take a graphic file from a web posting (upload) and determine what the "canvas" and or "page" color is?

Not sure if I'm expressing this correctly, but typically canvas colors are white and unless you save the file as a png with transparent background, the image is on a white block. I'd like to take an image that a user uploads via a web application, determine the canvas color and remove that color (make it transparent).

Is this possible?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Remove background color????

Post by snibgo »

IM can easily make any given colour transparent, but you have to tell IM which colour that is.
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: Remove background color????

Post by fmw42 »

If you know you have a reasonably constant background color and know where one pixel of that color may be (perhaps the upper left corner, i.e. 0,0), you can have IM do a fuzzy floodfill at that coordinate and make it some other color or transparent.

see

http://www.imagemagick.org/Usage/draw/#color
http://www.imagemagick.org/Usage/draw/#matte
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Remove background color????

Post by anthony »

Also see Simple background removal
http://www.imagemagick.org/Usage/masking/#floodfill

This will go though many of the issues you will probably face.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
lsintampa
Posts: 3
Joined: 2013-07-12T05:57:58-07:00
Authentication code: 6789

Re: Remove background color????

Post by lsintampa »

If I use this code:

-channel RGBA -bordercolor white -border 1x1 -fill black -floodfill +0+0 white -fuzz 20% -shave 1x1 -resize 176x176 +antialias

that works, I get a nice black background in place of the white

HOWEVER, just changing the -fill black to -fill none (or transparent) does not work and leaves the white background in place

-channel RGBA -bordercolor white -border 1x1 -fill none -floodfill +0+0 white -fuzz 20% -shave 1x1 -resize 176x176 +antialias

I'm at my wits end trying to figure this out. Adding -alpha set to either command will also fail

I'm doing this as a system call from a web application, so I can't really get error codes. All I know is it doesn't floodfill none or transparent - but it will black or any other color for that matter.

Works just as I would want, except I can't get the transparency to floodfill and I don't know why.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Remove background color????

Post by fmw42 »

try

-fill none -draw "matte 0,0 floodfill"

rather than

-fill none -floodfill ...

see
http://www.imagemagick.org/Usage/draw/#matte
lsintampa
Posts: 3
Joined: 2013-07-12T05:57:58-07:00
Authentication code: 6789

Re: Remove background color????

Post by lsintampa »

fmw42

THANKS - that worked!

Appreciate the quick response!
RyanBram
Posts: 30
Joined: 2013-03-25T22:55:10-07:00
Authentication code: 6789

Re: Remove background color????

Post by RyanBram »

How to tell ImageMagick to automatically choose the color from top-left pixel as transparent color.
In http://www.imagemagick.org/script/comma ... ransparent , and http://www.imagemagick.org/script/comma ... s.php#fill I don't see such argument

Regards.
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: Remove background color????

Post by fmw42 »

RyanBram wrote:How to tell ImageMagick to automatically choose the color from top-left pixel as transparent color.
In http://www.imagemagick.org/script/comma ... ransparent , and http://www.imagemagick.org/script/comma ... s.php#fill I don't see such argument

Regards.

Please do not add a new post to an old one. Better to start a new topic. Also identify your version of IM and Platform and explain what you are trying to do and/or provide your command line.

It is not clear what you are trying to do. Please clarify.

In one of the above posts, it was suggested to use

-fill none -draw "matte 0,0 floodfill"

The 0,0 is telling -draw to check the color at 0,0 and floodfill with transparency (color none)
RyanBram
Posts: 30
Joined: 2013-03-25T22:55:10-07:00
Authentication code: 6789

Re: Remove background color????

Post by RyanBram »

Sorry for my mistake.
I am not aware if reviving old thread even though related to the topic is against forum rules.
I'll be more careful next time.
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