replace white with transparency

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
nephish

replace white with transparency

Post by nephish »

Hey all,

i have searched throu this forum for the answer to this, but have not found one that works for me.
I have a couple hundred images that are gif, The image is of a circle on transparent background. There is other color stuff in the circle. My question is, how to i replace the white with transparency so i can overlay the image on another image on a website?
thanks for any tips
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: replace white with transparency

Post by fmw42 »

nephish wrote:Hey all,

i have searched throu this forum for the answer to this, but have not found one that works for me.
I have a couple hundred images that are gif, The image is of a circle on transparent background. There is other color stuff in the circle. My question is, how to i replace the white with transparency so i can overlay the image on another image on a website?
thanks for any tips

try

convert image.gif -transparent white result.gif (or use result.png)

or if not perfectly white, then try

convert image.gif -fuzz XX% -transparent white result.gif

where the smaller the %, the closer to true white or conversely, the larger the %, the more variation from white is allowed to become transparent.

http://www.imagemagick.org/script/comma ... ransparent
http://www.imagemagick.org/script/comma ... s.php#fuzz
nephish

Re: replace white with transparency

Post by nephish »

that is what i needed, the fuzz made a difference.
thanks very much.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: replace white with transparency

Post by anthony »

I have said this again and again.

Unless you are saving to GIF, using a color replacement with a fuzz factor is NOT a good way to change background to transparency. So unless the result is GIF, or the image has a limited palette (rare these days) don't use it!

The alternatives to this are however many, and varied, and often complex. It all depends on exactly the type of image and the edge effects that is involved.

A black edged object on a white background however should be simple to solve. by using a mask of 'what is fully-opaque' and the new -alpha shape operator.

I have NOT yet created an example of this simple case. Though the recent discussion on double masking methods is very close to this.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
manit
Posts: 123
Joined: 2009-01-30T22:31:26-07:00

Re: replace white with transparency

Post by manit »

thank you fmw42,
I tried '-transparent white' in my image , still some spots were left which were not exactly white . Then I added -fuzz 10% . Voila , I got what I wanted .
bam
Posts: 1
Joined: 2019-01-14T20:45:27-07:00
Authentication code: 1152

Re: replace white with transparency

Post by bam »

anthony wrote: 2008-12-03T19:18:14-07:00 Unless you are saving to GIF, using a color replacement with a fuzz factor is NOT a good way to change background to transparency. So unless the result is GIF, or the image has a limited palette (rare these days) don't use it!
Thanks Anthony.
Could you suggest what is the best sequence to make background transparency for this image?

Image
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: replace white with transparency

Post by anthony »

Compare it with a white image, and generate a mask, with anti-aliased transparent edges...

See http://www.imagemagick.org/Usage/masking/#semi-trans
Though it is simpler as the background is just white.

Basically work out what IS fully-opaque (foreground), and you know what is fully-transparent (background - white), and then try to work out appropriate alpha value and it corrected color, for the pixels that lie between the two parts. For sharp edges like this the color correction will typically the color of the nearest opaque pixel (simple morphology), so that is easy. Its working out the inside-outside and alpha values that is hard. See the example linked.

Of course it would be better if you can generate that gradient basied on the average color at each of the edge areas, though that would be difficult and may not be worth the effort.

I look forward to seeing what people on this forum come up with! :-)
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply