# How to make transparent all the white areas of an image

Let's take a sample image

Now I want to make transparent all the white areas of the image, so as to be able to copy/paste it above text

After the copy/paste only the color parts (non-white) of the figure should be covering the text.

I use version 9.0 of Mathematica.

Any suggestions?

• img = Import["https://i.stack.imgur.com/JLncI.png"], Export["transparent.png", SetAlphaChannel[img, 1 - ColorSeparate[img][[1]]]] seems to work. But it's not going to be generalizable to more complex images... Although, having just seen @C.E.'s answer, RemoveBackground seems like the right way to go in this case. – aardvark2012 Nov 26 '17 at 11:11
• @aardvark2012 When I run your code I get the following error message SetAlphaChannel::invalpha: Expecting an image or a number between 0 and 1 for the alpha channel specification instead of 1 – Vaggelis_Z Nov 26 '17 at 11:22
• What does 1 - ColorSeparate[img][[1]] give you? It gives me an image that I use as a mask... albeit a pretty hacky one. – aardvark2012 Nov 26 '17 at 11:37
• @aardvark2012 When I run 1 - ColorSeparate[img][[1]] I get 1 - the image in black and white. – Vaggelis_Z Nov 26 '17 at 11:41
• So SetAlphaChannel should be using that image as the mask... no idea what's going on there and why the error is appearing (it doesn't for me). But RemoveBackground seems to be working for you now, so I'd stick with that. – aardvark2012 Nov 26 '17 at 11:45

1. Use RemoveBackground[img, {"Background", White}]
2. Use SetAlphaChannel together with a mask
How to generate the mask would depend on the image. Binarize works in this case.
• When I use RemoveBackground[img, {"Background", White}] the exported JPEG image is not transparent, – Vaggelis_Z Nov 26 '17 at 11:21