Clear Filters
Clear Filters

Axes colour in figure changes after using saveas function to save as png

39 views (last 30 days)
I am trying to save a figure as a png which contains an image where NaN pixels are shown as black. I have done this by setting the axes colour to black and creating an alpha map where NaN values in the amage are set to zero. This appears correctly when the figure is diplayed but when I use the saveas function to save the figure as a png the black pixels change to white. If I save the figure manually as a png they stay black but this needs to be repeated for many images so this is impractical.
How can I preserve the axes colour when saving as a png? See example code with dummy image and alphamap below:
a = rand(100,100); % create random array as test image
a(a<0.5) = nan; % threshold image to introduce nan pixels
figure()
testplot = axes(); % create axes
imagesc(testplot,a); % show test image
aMap = rand(100,100); % create random array for alpha map
aMap(isnan(a)) = 0; % set all nan pixels in a as zero in the alpha map
alpha(testplot,aMap); % apply alpha map to plot
set(gca,'color','k'); % set background colour to black
% save as png and as matlab figure
saveas(gcf,'testimage.png')
saveas(gcf,'testimage.fig')

Accepted Answer

Dario Angelone
Dario Angelone on 3 Apr 2023
A previously deleted answer suggested trying
exportgraphics(gcf,'testimage.png','ContentType','image','Resolution',600)
This is producing the desired output.

More Answers (1)

Jack
Jack on 30 Mar 2023
When you save the figure using the saveas function, MATLAB uses a default resolution of 150 DPI (dots per inch) to create the PNG image. The default renderer used by MATLAB to create PNG images is the OpenGL renderer, which does not always produce consistent results when dealing with transparency and alpha maps. This is likely the cause of the issue you are experiencing, where the NaN pixels in the image are shown as white instead of black.
To fix this issue, you can try using a different renderer when saving the figure as a PNG. One option is to use the -r option with the print function to set a higher resolution and a different renderer. For example, you can try setting the renderer to 'Painters' and the resolution to 300 DPI using the following code:
% save as png using the print function with higher resolution and different renderer
print(gcf, 'testimage.png', '-dpng', '-r300', '-painters');
This code saves the figure with a resolution of 300 DPI and uses the 'Painters' renderer instead of the default OpenGL renderer. The 'Painters' renderer is a vector-based renderer that is better suited for creating high-quality images with transparency and alpha maps.
Alternatively, you can try using the exportgraphics function introduced in MATLAB R2020a to save the figure as a PNG with transparency. This function allows you to specify the format and resolution of the output image, as well as any additional options such as setting the background color. Here is an example of how to use the exportgraphics function to save the figure as a PNG with transparency:
% save as png using exportgraphics function with transparency
exportgraphics(gcf, 'testimage.png', 'Resolution', 300, 'BackgroundColor', 'k', 'ContentType', 'image/png');
This code saves the figure with a resolution of 300 DPI, sets the background color to black, and specifies the output format as PNG. The resulting PNG image should preserve the black background color and the NaN pixels in the image should be shown as transparent.
  2 Comments
Dario Angelone
Dario Angelone on 3 Apr 2023
The first code still produces an image with a white background and the second generates the error:
Error using exportgraphics
'ContentType' value must be one of 'vector', 'image', or 'auto'.

Sign in to comment.

Categories

Find more on Printing and Saving in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!