필터 지우기
필터 지우기

Saving an axes as jpg file using saveas

조회 수: 44 (최근 30일)
nl2605
nl2605 2013년 7월 18일
답변: Prajwol Tamrakar 2023년 10월 6일
Hallo All
I have been trying to save an axes object on an gui as jpg file but it takes up weird pictures, like sometime the photo has only the centre part of the axes and sometimes it doesn't save the picture of the axes at all instead it takes picture of the pushbuttons besides the axes (in the GUI). Have been trying to set this error for a long time.
Thanks a lot.

채택된 답변

Dishant Arora
Dishant Arora 2013년 7월 18일
F = getframe(handles.axes1);
Image = frame2im(F);
imwrite(Image, 'Image.jpg')
  댓글 수: 5
Adryan Fauzi
Adryan Fauzi 2023년 7월 5일
hi Dishant Arora, i have tried this code and successfully export my axes to image, but my image doesn't include axes title and axis description. how do I get the exported image to include both of those things?
DGM
DGM 2023년 7월 5일
편집: DGM 2023년 7월 5일
If you want to capture the axes, capture the axes. If you want to capture everything in the figure, capture the figure.
% some plot in an axes in a figure
x = 1:10;
plot(x)
title('blah')
xlabel('blah')
ylabel('blah')
% capture just the axes
axscreenshot = frame2im(getframe(gca));
imwrite(axscreenshot,'axscreenshot.png')
% capture the figure
figscreenshot = frame2im(getframe(gcf));
imwrite(figscreenshot,'figscreenshot.png')
If you have multiple axes or figures, or if you're building a GUI, use the appropriate handles instead of gca/gcf. If trying to do this inside a GUI, there may be other complications.
Also, unless you have some extraordinary needs, don't use JPG. For images like this, you'll get garbage results, and the files will typically be larger than a lossless PNG.
This is a 3x enlarged screenshot saved as PNG. The lines are crisp and colorful. The text is clear.
This is the same image saved as JPG and enlarged. The lines and text are blurry and surrounded by artifacts. The line colors are all muddy. The file size is tripled. Why would you intentionally do this?

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Prajwol Tamrakar
Prajwol Tamrakar 2023년 10월 6일
When using App Designer and you do not have the latest version 2020 or later, it is literally IMPOSSIBLE to save an image from the figure (app.UIAxes). It seems easy previously (of course easy when using GUIDE or just simple code, but Not for App Designer 2019a version). I wasted lots of hours trying to find a solution for this. The best solution ever found was - just get the screen short using the following code.
% Take screen capture
robot = java.awt.Robot();
pos = [0 0 400 400]; % [left top width height]
rect = java.awt.Rectangle(pos(1),pos(2),pos(3),pos(4));
cap = robot.createScreenCapture(rect);
% Convert to an RGB image
rgb = typecast(cap.getRGB(0,0,cap.getWidth,cap.getHeight,[],0,cap.getWidth),'uint8');
imgData = zeros(cap.getHeight,cap.getWidth,3,'uint8');
imgData(:,:,1) = reshape(rgb(3:4:end),cap.getWidth,[])';
imgData(:,:,2) = reshape(rgb(2:4:end),cap.getWidth,[])';
imgData(:,:,3) = reshape(rgb(1:4:end),cap.getWidth,[])';
% Show or save to file
imshow(imgData)
imwrite(imgData,'out.png')

카테고리

Help CenterFile Exchange에서 Display Image에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by