Saving images with text with desired dimensions (problem with getframe function)

1 view (last 30 days)
Hi,
I am working on programming a cognitive experiment that involves generating background image scenes with a letter pasted on top of them in random locations. For the experiment, the dimension of the image must be kept consistent (the size of the screen), and I am having trouble doing this.
The image dimensions stay consistent when I have the background image without text, but become random as soon as I put the text object on. Here is an abbreviated section from my code for where the text and resizing issues occur:
%First, I load the background image (I originally tried pasting the text ontop of the image and saving it all at once, but the text would only show in in the MATLAB figure and not in the jpg)
im = imread(['my_image' '.jpg']);
%Here are the dimensions I want:
figure('Position',[0,0,1280,1024]);
imshow(im);
%Ignore this line, this is just pasting the text on from some earlier coding, but you get the idea...
text(scene_info(scene_counter,9),scene_info(scene_counter,10),ascii_biglet,'FontName',ascii_font,'FontSize',200, 'Color',[scene_info(scene_counter,15)/255 scene_info(scene_counter,16)/255 scene_info(scene_counter,17)/255], 'Units','pixels');
%It appears that the dimensions get messed up while using getframe:
f_ = getframe(gca);
%So I try to reset the image size to what I had before
set(f_,'Position',[0 0 1280 1024]);
%and update my background image to include the text
im = frame2im(f_);
%I try resetting the size again just for good measure, but this should be redundant:
set(gcf,'Position',[0 0 1280 1024]);
%and then rewrite my image to include the text
imwrite(im,['my_image '.jpg']);
And the image now saves with the text, but the dimensions it saves it in are random and not what I inputted - usually a few hundred pixels off.
Am I missing something, and is there a way to get around this resizing issue using getframe?
Thanks so much for any ideas and insight!
-Jamie

Answers (0)

Community Treasure Hunt

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

Start Hunting!