How to save a custom-tailored figure (image) into a jpeg (or other formats) files

1 次查看(过去 30 天)
I generate a 2D array (double) in a gui program and I use imagesc to make a figure. Then the user has options to change and custom-make the figure (axis equal, tight, zoom in, colormap, change CLim, etc.). I would like to allow the user to save this custom-made figure as a jpg or other formats without the boarders and axes. I used imwrite, but I don't know how to apply the custom-made options. I would be grateful if anyone can provide me an answer. Thanks in advance.

回答(1 个)

Hari
Hari 2024-9-3
编辑:Hari 2024-9-3
Hi Ali,
I understand that you have a GUI program that generates a 2D array and displays it using imagesc. Users can customize the figure's appearance, and you want to save this customized figure as an image file without borders and axes.
I assume you want to preserve the customizations (like zoom, colormap, axis adjustments) made by the user before saving the figure.
  • Capture the Figure: Use "getframe" to capture the currently displayed figure, including all user-made customizations.
% Assume 'hFig' is the handle to your figure
frame = getframe(hFig);
img = frame.cdata;
  • Remove Borders and Axes: If you want to remove axes and borders, ensure they are turned off before capturing.
% Turn off axes and borders
set(gca, 'Visible', 'off');
  • Save the Image: Use "imwrite" to save the captured image data to a file in the desired format (e.g., JPEG).
% Save the image as a JPEG file
imwrite(img, 'custom_figure.jpg');
  • Restore Axes and Borders: If needed, restore the axes and borders after saving.
% Restore axes visibility
set(gca, 'Visible', 'on');
  • Provide User Options: Allow the user to choose the file format and location using "uiputfile".
% Let the user choose file format and location
[file, path] = uiputfile({'*.jpg';'*.png';'*.tiff'}, 'Save Image As');
if ischar(file)
imwrite(img, fullfile(path, file));
end
References:
Hope this helps!

类别

Help CenterFile Exchange 中查找有关 Lighting, Transparency, and Shading 的更多信息

标签

产品


版本

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by