Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save figure with transparent background

I have a plot in Matlab and am setting the background to transparent by:

set(gcf, 'Color', 'None');
set(gca, 'Color', 'None');

When I try to save the image (from the viewer), I save as a ".png", but it saves with a white background. How can I save it with the transparent background?

like image 913
Aly Avatar asked Apr 21 '13 18:04

Aly


People also ask

Why won't my image save with a transparent background?

You may be used to saving image files for web use as JPEGs, but JPEGs don't support transparent backgrounds. So, instead, you'll need to use a format such as GIF, TIF or, ideally, PNG. The PNG file is small enough for use online but still delivers high quality with transparency as well.

How do I save a PNG with a transparent background?

Simply click the “Download' dropdown menu, then check the box that says “Transparent background.” Ensure that you don't have a background image loaded on the canvas.

Can a JPEG have a transparent background?

The JPEG format doesn't support transparency. But we can create our own transparency using a second image as an alpha channel.


1 Answers

It is disappointing but, MATLAB's default saveas and print commands cannot deal with transparent things very well. You'll have to save it with some background and then convert it either through imread/imwrite or some other tool.

There are some tools that might be helpful:

  • Export fig http://www.mathworks.com/matlabcentral/fileexchange/23629
  • svg export http://www.mathworks.com/matlabcentral/fileexchange/7401-scalable-vector-graphics-svg-export-of-figures

I prefer vector graphics, so use svg exports when transparency is needed. If indeed you have a bitmap, use imwrite(bitmapData, 'a.png', 'png', 'transparency', backgroundColor).

like image 188
Memming Avatar answered Sep 29 '22 14:09

Memming