File Exchange

image thumbnail

Add Text To Image

version 1.2 (7.72 KB) by

Overlays rasterized text on an image matrix.



View License

Sometimes it's nice to be able to place text on an image and know precisely where it will render irrespective of the figure size and shape and other whims of the text() function. This submission achieves that by directly overwriting part of the image matrix with rasterized text generated using bitmapped fonts. It allows the use of any system font that text() will take. Text size, colour and position can also be controlled.

This code can undoubtedly be tweaked (or maybe just rewritten) to produce more aesthetically pleasing results. Feel free to build upon it!

Comments and Ratings (16)


Phillip (view profile)

Daniel Warren

Hi Tanmay,

I'm afraid that I haven't been able to reproduce that - I've just downloaded the .zip from here and run the demos in MATLAB R2015b without issue.

My understanding is that P-files are used to distribute executable MATLAB code without disclosing the exact source code. All the code here is in human-readable .m format. A quick Google suggests that P-files from one MATLAB version might not be compatible with other versions - so perhaps this is your issue?


Hi Daniel,

The latest version is giving corrupt P file error. Any way to fix that?


Very useful!!

Daniel Warren

Hi Jeff,

I'm not interesting in adding that myself right now, but you could consider the following horrible hack. There are a bunch of subscript and superscript characters in Unicode, which you can copy and paste if any are appropriate - find them here:

Alternatively, I don't think it would be too hard to modify the function yourself. Rasterize the full-size text using RasterizeText, and the subscript/superscript parts separately in a slightly smaller font size. Concatenate the resulting text masks, offsetting the subscript/superscript vertically as appropriate, and then merge with the image following AddTextToImage. The most complicated part will probably be deciding how to indicate sub/super in the input string, and then parsing that info appropriately.

Hope this helps.


Xiaoyu Wu

Hi Daniel,

Thank you very much for the file. Can I ask something about the input text ? At the moment, I can not write any subscript or superscript in the contents of the input text using your file. Can you or someone kindly modify the code to add the function ?

Very Best,

Daniel Warren

Vincenzo: You have modified the function BitmapFont as suggested in Robert's comment below, which relies on an undocumented (and now retired) function. Return to the original code and all should be fine.

Daniel Warren

In response to Thai's comment:

Smoothing would be entirely possible. I don't have an interest in adding it myself, but I think this is all you'd need to do:
- modify BitmapFont to store characters as uint8 instead of logical by removing line 68 and 69.
- create the output of RasterizeText as a uint8 by changing line 46, and replace the boolean logic in lines 72-74 with addition
- rewrite lines 92-96 assuming TextMask is an alpha map instead of a simple mask

Smoothed text with borders would be more of a challenge.


Kevin (view profile)

Errors with matlab r2014b:

Warning: The undocumented 'hardcopy' function will be removed in a future release of MATLAB; use the documented 'print' command instead.
> In C:\Program Files\MATLAB\R2014b\toolbox\matlab\graphics\hardcopy.p>hardcopy at 39
In BitmapFont at 57
In Interfaccia_Ris>apriIM at 182
In Interfaccia_Ris>Interfaccia_Ris_OpeningFcn at 46
In gui_mainfcn at 220
In Interfaccia_Ris at 14
In Kernel_NEW_Montecarlo_ClassificazioneSar at 446
Error using inputcheck>LocalCheckOption (line 342)
Illegal option '-OpenGL' given.

Error in inputcheck (line 86)
opIndex = LocalCheckOption( cur_arg, options );

Error in print (line 156)
[pj, devices, options ] = inputcheck( pj, inputargs{:} );

Error in hardcopy (line 62)

Error in BitmapFont (line 57)
Bitmap = hardcopy(fighandle, path, '-dOpenGL', '-r0');

Can you update your script?
Thank you so much!

Thai V. Hoang

Nice functions. Do you have any solution to smooth the text contour when font size is very high as 150?



Robert (view profile)

Just what I needed, thanks. Suggested change to prevent text figure from being visible (lines from BitmapFont.m):

%fighandle = figure('Position',[50 50 150+Size 150+Size],'Units','pixels','Color',[1 1 1]); % original
fighandle = figure('Visible','Off','Position',[50 50 150+Size 150+Size],'Units','pixels','Color',[1 1 1]);

%Bitmap = getframe(gcf); % original
Bitmap = hardcopy(fighandle, path, '-dOpenGL', '-r0');

%Bitmap = mean(Bitmap.cdata,3); % original
Bitmap = mean(Bitmap,3);

B Treeby

Nice function, exactly what I was looking for! I had to add a drawnow after the call to text in BitmapFont.m to prevent the same letter being captured several times by getFrame. It would also be nice to remove the reliance on the padarray function and the image processing toolbox.

Daniel Warren

@Sav: That's not something I thought about, but how's about this?

Text = 'Test Text';
Position = [100 100];
FontName = 'Arial';
FontSize = 32;
Color = [1 1 0];
Border = 1; % thickness in pixels

Mask = AddTextToImage(false(size(Image(:,:,1))),Text,Position,1,FontName,FontSize);
Outline = logical(imdilate(Mask,ones(1+2*Border,1+2*Border))-Mask);
TextOnImage = AddTextToImage(Image,Text,Position,Color,FontName,FontSize);

for i = 1:3
tmp = TextOnImage(:,:,i);
tmp(Outline) = 0;
TextOnImage(:,:,i) = tmp;

Sav Deb

Is it possible with this script to create an outlined text?



Included drawnow call in BitmapFont and removed dependency on Image Processing Toolbox as suggested by B Treeby. Also added a separate version which adds a border to the text with custom colour and thickness.


Wrote a demonstration script to produce the screenshot, which revealed a vertical clipping bug when using large fonts - this has been fixed. Also avoided some error conditions when requested position is almost or completely outside the image.

MATLAB Release
MATLAB 7.14 (R2012a)

Inspired: xic draw

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video