Page 1 of 1

Replace white with transparent?

Posted: 2008-04-09T13:06:18-07:00
by vbsaltydog
Is there a way to have IM replace the color #ffffff with transparent over an entire image (non-contiguous) ?

Re: Replace white with transparent?

Posted: 2008-04-09T13:15:31-07:00
by fmw42
convert inputimage -transparent white outputimage

or

convert inputimage -transparent "#ffffff" outputimage

Re: Replace white with transparent?

Posted: 2008-04-09T14:04:02-07:00
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.

Re: Replace white with transparent?

Posted: 2008-04-09T14:33:36-07:00
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?

Re: Replace white with transparent?

Posted: 2008-04-09T16:05:31-07:00
by rmagick
Check the -fuzz option.

Re: Replace white with transparent?

Posted: 2008-04-09T16:11:53-07:00
by vbsaltydog
rmagick wrote:Check the -fuzz option.
I am unclear of what the distance value should be. Any advise?

Re: Replace white with transparent?

Posted: 2008-04-09T16:57:51-07:00
by rmagick
I advise trial and error.

Re: Replace white with transparent?

Posted: 2008-04-09T17:02:24-07:00
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?

Re: Replace white with transparent?

Posted: 2008-04-09T17:41:46-07:00
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!

Re: Replace white with transparent?

Posted: 2008-04-09T18:31:45-07:00
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.

Re: Replace white with transparent?

Posted: 2008-04-09T18:44:12-07:00
by magick
The fuzz option accepts percent values as well, for example, -fuzz 15%.

Re: Replace white with transparent?

Posted: 2008-04-09T21:01:31-07:00
by vbsaltydog
Thanks. I found an acceptable fuzz percentage.

Your help is appreciated.

Re: Replace white with transparent?

Posted: 2008-05-17T23:45:29-07:00
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