"print figure" to variable... getframe, with better resolution...

Short version: I want to read my current figure into a variable, with settable resolution. getframe only uses the screen resolution.
Long version: I plot lots of data, and generate "publication ready" png's often... which demand higher resolution (300dpi or better). I'd also like to change some defaults between what's on the screen and the .png file (cropping, color mods, transparency, in addition to the critical resolution).
My curret setup is: scatter(x,y,S,C); print('-dpng','-r300','plot.png'); imdata=imread('plot.png'); ...some manipulations to imdata... imwrite(imdata,'plot.png');
The above code (which saves the file, reads it, and resaves) is not only embarrassing, but slow for multiple images.
Any guidance is appreciated! PS: I do not have any toolboxes.

Answers (2)

Creating a copy of a figure in a higher resolution is time consuming, because a lot of work has to be done. You can reduce the time required by print by writing to a SSD or RAM-disk. Which OS are you using?
Another way is using the undocumented hardcopy, which is the core function of print. hardcopy replies an RGB array, but it demands for some specific modifications of the figure - otherwise Matlab crashs. I give a short example, which works with Matlab 6.5, 7.8 and 7.13 without any guarantee:
ResolutionStr = sprintf('-r%d', round(Resolution));
% Prepare figure for hardcopy:
drawnow;
fig_Renderer = get(FigH, 'Renderer');
fig_Paperposmode = get(FigH, 'PaperPositionMode');
fig_PaperOrient = get(FigH, 'PaperOrientation');
fig_Invhardcopy = get(FigH, 'InvertHardcopy');
set(FigH, ...
'PaperPositionMode', 'auto', ...
'PaperOrientation', 'portrait', ...
'InvertHardcopy', 'off');
% Create hard copy in high resolution:
% Simulate PRINT command (save time for writing and reading image file):
% Set units of axes and text from PIXELS to POINTS to keep their sizes
% independent from from the output resolution:
% See: graphics/private/preparehg.m
root_SHH = get(0, 'ShowHiddenHandles');
set(0, 'ShowHiddenHandles', 'on');
text_axes_H = [findobj(FigH, 'Type', 'axes'); ...
findobj(FigH, 'Type', 'text')];
pixelObj = findobj(text_axes_H, 'Units', 'pixels');
fontPixelObj = findobj(text_axes_H, 'FontUnits', 'pixels');
set(pixelObj, 'Units', 'points');
set(fontPixelObj, 'FontUnits', 'points');
% Set image driver:
if strcmpi(fig_Renderer, 'painters')
imageDriver = '-dzbuffer';
else
imageDriver = ['-d', fig_Renderer];
end
fig_ResizeFcn = get(FigH, 'ResizeFcn');
set(FigH, 'ResizeFcn', '');
% "Normal" is the only erasemode, which can be rendered!
% See: NOANIMATE.
EraseModeH = findobj(FigH, 'EraseMode', 'normal', '-not');
EraseMode = get(EraseModeH, {'EraseMode'});
set(EraseModeH, 'EraseMode', 'normal');
% Get image as RGB array:
high = hardcopy(FigH, imageDriver, ResolutionStr);
% Restore units of axes and text objects, and EraseMode:
set(pixelObj, 'Units', 'pixels');
set(fontPixelObj, 'FontUnits', 'pixels');
set(EraseModeH, {'EraseMode'}, EraseMode);
set(0, 'ShowHiddenHandles', root_SHH);
set(FigH, 'ResizeFcn', fig_ResizeFcn);

8 Comments

The image format you choose to write to disk in also makes a difference. Sometimes the compression may be slower than writing a larger file.
The byte-order in the files matters also. I think BMPs are the fastest solution, if the disk is fast.
The posted code is from a function I have written for the creation of anti-aliased version of GETFRAME I'm using for the creation of an animation: For the export of 10'000 images, I need the maximum speed.
(years later) I'm working to solve a similar problem, trying to save large images from figure into a file. I'm not sure how to implement your code example Jan, or if it's even the best solution (or functional) in r2018b
Hi @Jan when running the code matlab says
Warning: The EraseMode property is no longer supported and will error in a future release.
Do you have an updated version of the code working with R2019?
Thank you very much for all your work!
For R2019b try to remove all lines, which contain "EraseMode".
Many thank @Jan for fast reply! I removed those lines but now it says
Unrecognized function or variable 'hardcopy'.
It seems like it is no longer a function in MATLAB... any workaround? Maybe it is better if I open a new question?
Try to replace hardcopy() by
high = print('-RGBImage');
I cannot try it by my own currently.
Thank you @Jan very kind!
That print command substitute all the other lines of code right?
The drawback is that since it is print it is slow

Sign in to comment.

Take a look at export_fig which is a user-submitted file on File Exchange. I've used it, and it's good.

2 Comments

export_fig is very good, but as far as I can see, it uses the slow way of print->imread also.
Oops, I didn't see the question about "being slow". I guess I'm slow.

Sign in to comment.

Categories

Find more on Graphics Performance in Help Center and File Exchange

Asked:

Kip
on 25 Feb 2012

Commented:

on 26 Mar 2021

Community Treasure Hunt

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

Start Hunting!