MATLAB Answers

Mr M.

How to plot png images with transparent background?

Asked by Mr M.
on 12 Apr 2018
Latest activity Commented on by Cris LaPierre
on 11 Sep 2019
I've just used image(), and the transparent area are shown with black colour by default, however I need white background. I tried to use set(gcf,'color','white') but this cannot help.


Sign in to comment.

2 Answers

Answer by Walter Roberson
on 12 Apr 2018
 Accepted Answer

When you image() pass in 'AlphaData', and the transparency data. The alpha needs to be floating point (not logical) in the range 0 to 1 and the same size as the image.

  1 Comment

[img, map, alphachannel] = imread('YourImage.png');
image(img, 'AlphaData', alphachannel);

Sign in to comment.

Answer by Mr M.
on 12 Apr 2018

OK, but how to read the png to keep transparent background?

  1 Comment

Images get displayed in an axes. Even if the image doesn't have a background, it will at least originally display in an axes with a background color set to white. You can turn that off using
axis off

Sign in to comment.