How to export transparent raster plots?

33

11

Is there an easy way to get transparent plots that work with Save Graphic As using a compatible file format such as GIF or PNG? I tried Background->Opacity[0,White] and Background->None, yet the exported PNGs appear to still have a white background.

Emre

Posted 2012-05-09T07:57:50.900

Reputation: 1 630

2If questions could receive bounties, I would give you 500 right now. I couldn't find the answer to this for my life! Isn't it ironic I posted it myself 4 years ago? – Szabolcs – 2016-02-23T09:44:27.840

Answers

37

Instead of using Save Image As... you can use the Export command, like this:

Export["transparent.png", Graphics[Circle[]], Background -> None]

For this to work it is important that Background -> None is also set inside Graphics. This is the default though so unless you changed it, it should be fine.

Similarly, to convert to an image with alpha channels, use

Rasterize[Graphics[Circle[]], "Image", Background -> None]

The output of this can be saved even using Save Image As..., but this effort needed is the same because of the Rasterize function.

Szabolcs

Posted 2012-05-09T07:57:50.900

Reputation: 213 047

Does the Save Graphic As command have a bug? – Emre – 2012-05-09T08:42:09.510

1@Emre No, it simply doesn't provide a way to change as many options as Export does. The background is white by default when exporting. You need to change it to transparent, and this is possible with Export but not with the simpler Save Graphic/Image As.... People typically need the white background (imagine someone putting a figure with black lines on a black-background webpage), so this is the default. – Szabolcs – 2012-05-09T08:43:49.067