How to export transparent png in app designer?

조회 수: 8(최근 30일)
亦实 胥
亦实 胥 2021년 12월 1일
답변: Richard Quist 2021년 12월 3일
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.

답변(1개)

Richard Quist
Richard Quist 2021년 12월 3일
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]);

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by