How to export transparent png in app designer?

9 vues (au cours des 30 derniers jours)
亦实 胥
亦实 胥 le 1 Déc 2021
Now i used
[f,p]=uiputfile({'*.eps'},'创建图片','chu');
exportgraphics(app.UIAxes_2,[p,f],'ContentType','vector','BackgroundColor',"none",'Resolution',500)
In this way ,i can get a eps with a white backgound panel and a transparent curve. How to export only the transparent curve;
In the documentation of 'exportgraphics' ,i found this code could not export a transparent png, How to export transparent png?
% Background color, specified as 'current', 'none', an RGB triplet, a hexadecimal color code, or a color name. The background color controls the color of the margin that surrounds the axes or chart.
% A value of 'current' sets the background color to the parent container's color.
% A value of 'none' sets the background color to transparent or white, depending on the file format and the value of ContentType:
% Transparent — For files with ContentType='vector'
% White — For image files, or when ContentType='image'
% When ContentType='auto', MATLAB sets the background color according to the heuristic it uses to determine the type content to save.
% Alternatively, specify a custom color or a named color.
There is an additional code 'export_fig', but it can only export the content in the current figure.

Réponses (1)

Richard Quist
Richard Quist le 3 Déc 2021
As you noted, the exportgraphics function in MATLAB does not support generating transparent PNG files.
If the EPS format is acceptable for your workflow you could try setting the uiaxes color to 'none' before exporting which should remove the "white backgound panel" you noticed.
% remember the original background color and set it to 'none' temporarily
origColor = app.UIAxes_2.Color;
app.UIAxes.Color = 'none';
exportgraphics(app.UIAxes_2,[p,f],'ContentType','vector','BackgroundColor',"none",'Resolution',500)
% restore the original background color
app.UIAxes_2.Color = origColor;
If you need to work with the PNG format you could try exporting to a PNG file, reading that file back in with the imread function, and then rewriting it with imwrite using one of the transparency-related options ('Alpha' or 'Transparency') available for the PNG format.
% read in the existing image file
cdata = imread('output.png');
% write image back out treating all white pixels as transparent
imwrite(cdata, 'transparent.png', 'Transparency',[1 1 1]);

Catégories

En savoir plus sur Printing and Saving dans Help Center et File Exchange

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by