Replace white with transparent?

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
vbsaltydog

Replace white with transparent?

Post by vbsaltydog »

Is there a way to have IM replace the color #ffffff with transparent over an entire image (non-contiguous) ?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Replace white with transparent?

Post by fmw42 »

convert inputimage -transparent white outputimage

or

convert inputimage -transparent "#ffffff" outputimage
vbsaltydog

Re: Replace white with transparent?

Post by vbsaltydog »

Thanks, it worked partially. I had to save as a gif for the transparency to save which is no big deal but the transparency is badly pixelated around the image edges. Is there a way to soften this or match the #ffffff less strictly?

Thanks again.
vbsaltydog

Re: Replace white with transparent?

Post by vbsaltydog »

Or is there a way to convert an image to something with clean edges, like line art, that can then be used with background transparency so it shows cleanly when displayed on a dark canvas?
rmagick
Posts: 245
Joined: 2006-03-16T17:30:48-07:00
Location: Durham, NC, USA

Re: Replace white with transparent?

Post by rmagick »

Check the -fuzz option.
vbsaltydog

Re: Replace white with transparent?

Post by vbsaltydog »

rmagick wrote:Check the -fuzz option.
I am unclear of what the distance value should be. Any advise?
rmagick
Posts: 245
Joined: 2006-03-16T17:30:48-07:00
Location: Durham, NC, USA

Re: Replace white with transparent?

Post by rmagick »

I advise trial and error.
vbsaltydog

Re: Replace white with transparent?

Post by vbsaltydog »

I don't even know where to begin. What is a distance from #ffffff and what would be a distance increment from the first example distance?
rmagick
Posts: 245
Joined: 2006-03-16T17:30:48-07:00
Location: Durham, NC, USA

Re: Replace white with transparent?

Post by rmagick »

I don't either, so if I were doing this, I'd try -fuzz 50. If that didn't help, I'd try -fuzz 500. Not enough? -fuzz 5000. Eventually you'll find some number that is either perfect or too big. If it's too big, try something halfway between too big and not enough.

-fuzz 50 - not enough
-fuzz 500 - too big
-fuzz 250 - too big
-fuzz 125 - pretty close
-fuzz 100 - not enough
-fuzz 112 - on the money!
vbsaltydog

Re: Replace white with transparent?

Post by vbsaltydog »

Thanks. I didn't know if the distance was set as simply as that or if it was a matrix or other complex math equation.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Replace white with transparent?

Post by magick »

The fuzz option accepts percent values as well, for example, -fuzz 15%.
vbsaltydog

Re: Replace white with transparent?

Post by vbsaltydog »

Thanks. I found an acceptable fuzz percentage.

Your help is appreciated.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Replace white with transparent?

Post by anthony »

The highly pixelated is a result of two things.

First you are replacing pure white with transparent. No semi-transparent pixels will be generated to smooth the edges or make it cleaner looking.
See IM examples, Channels, Masks and Transparency,
Transparency Masking an Image
http://imagemagick.org/Usage/channels/#mask_creation

Secondly GIF can not handle semi-transparency, so even if you create a nice clean looking edge using semi-transparent pixels, it will still come out horible, unless you smooth those semi-transparent pixels with a color close to the intended backgound the image will be used on.
See IM examples Common Image Formats, GIF,
GIFs on a solid color background
http://imagemagick.org/Usage/formats/#bgnd
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply