How do I control the image size when printing to a file?

53 views (last 30 days)
I wanted to save a image of 1280 by 1080 to a file in the png format. The solution I found to do this, by reading the Matlab documentation was
% trick Matlab to save the file at correct size:
% set unit to inches
% set resolution at 100 dots/inch
% set X size to 12.8 -> image size will be 12.8*100 = 1280
% set Y size to 10.8 => image size will be 10.8*100 = 1080
set(gca,'position',[0 0 1 1],'units','normalized');
%set(gcf,'PaperUnits','inches','PaperPosition',[0 0 4.8 2.4]);
set(gcf,'PaperUnits','inches','PaperPosition',[0 0 12.8 10.8]);
print(hF,'-dpng','-r100','out.png');
The code does not include the portion in which I generated an image of exactly 1280 by 1080 pixels. The problem is that the output image was 1281 by 1080. One pixel to many for the width. For smaller sizes, like 480 by 240 the output was of the required dimensions. But for 800 by 600 and for 1280 by 1080, the same problem: 1 pixel to many for the width.
Can this be solved in some manner? I need the output at exactly the resolution, one extra column is no good!

Answers (3)

Guillaume
Guillaume on 30 Sep 2015
If the image you want to save is actually an image and not a figure (i.e. a 1280x1080 array of pixels and not a vector plot) then use imwrite.

Horia Popa
Horia Popa on 30 Sep 2015
I have an image, plot over it, then I want to save it. To do the plotting I am putting it on a figure, so you might say I have a figure.

Image Analyst
Image Analyst on 30 Sep 2015
  1 Comment
Horia Popa
Horia Popa on 1 Oct 2015
Tried
export_fig out.png -native -a1;
% and
export_fig out1.png -r100 -a1;
and I've got even larger images (1285 by 1084 by 24) respectively (893 by 753 by 24). According to the documentation, the -native option should produce the original size in pixels, and the second seems to depend on the size it has on the screen (as a warning I got that it was scaled at 67% from the original size because it is to big for the screen, and that is close of the output dimensions). So, no solution yet.
The 24 is BPP so 3 bytes as the original, but the size is worse than the first variant with print.
Maybe I didn't understood all the options, still I have doubts that is the case.
The single reasonable solution seems for me to save it as it is then reopen it and cut off the extra lines/columns.
But this is a hack and it is not possible all the time.

Sign in to comment.

Categories

Find more on Display Image in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!