필터 지우기
필터 지우기

wrong output image size using exportgraphics()

조회 수: 47 (최근 30일)
Dimitrii Nikolaev
Dimitrii Nikolaev 2020년 5월 12일
답변: Jianeng Wang 2022년 12월 16일
Description
I need to export a sequence of figures with specific size in pixel. The function exportgraphics() generates a file with dimensions almost as specified, but not exactly. How can I export a png file with exportgraphics with exact with and heigth in px?
how to reproduce the problem:
1) Export .png using print('-dpng'):
figure
surf(peaks)
output_size = [500 300];
resolution = 300;
set(gcf,'paperunits','inches','paperposition',[0 0 output_size/resolution]);
print('test.png','-dpng',['-r' num2str(resolution)]);
Produces a 500x300 px test.png file.
2) Export .png using exportgraphics(...,'Resolution',...):
figure
surf(peaks)
output_size = [500 300];
resolution = 300;
set(gcf,'Units','inches','Position',[0 0 output_size/resolution]);
exportgraphics(gcf,'test_exportgraphics.png','Resolution',resolution);
Produces a 497x277 px test_exportgraphics.png file.
  댓글 수: 1
Dimitrii Nikolaev
Dimitrii Nikolaev 2020년 5월 12일
getframe and print Functions
When using the getframe function (or the print function with the -r0 option) on a high-DPI system, the size of the image data array that MATLAB returns is larger than in previous releases. Additionally, the number of elements in the array might not match the figure size in pixel units. MATLAB reports the figure size based on device-independent pixels. However, the size of the array is based on the display DPI.
so how am I supposed to save my figure in desired resolution?

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

답변 (2개)

Jianeng Wang
Jianeng Wang 2022년 12월 16일
I have a workaround for keeping the output image size the same from a sequence of figures.
You can use exportgraphics to output the images from figures first. Then imread those images, and set a specific image size you want (e.g., the 1st image's size). Next using imresize for each image to the specific image size. Finally imwrite the resized image with the same name when you exportgraphics to replace the existing image files.

Harsha Priya Daggubati
Harsha Priya Daggubati 2020년 5월 15일
편집: Harsha Priya Daggubati 2020년 5월 18일
Hi,
This link gives idea about the customisation options available with exportgraphics method in MATLA 2020a.
  댓글 수: 3
Harsha Priya Daggubati
Harsha Priya Daggubati 2020년 5월 18일
Hi,
Even I get the png with 297X497X3 as dimensions.I guess this is the reason the size of the image will be approximately equal to specified size. Workaround would be saving the content as vector graphics and resize the resulting file as specified in the documentation.
I brought this issue to the notice of concerned people, will get back to you as soon as I get a response.
Harsha Priya Daggubati
Harsha Priya Daggubati 2020년 5월 19일
Hi,
Following my reply, Currently exportgraphics saves the content closely cropped around the onscreen size(factoring in the requested output resolution), control over the generated outputs' dimensions and how much cropping is performed is not supported as of now. It might be considered in future releases.
As of now to allow the required customisations, you can continue using print.

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

카테고리

Help CenterFile Exchange에서 Printing and Saving에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by