File Exchange

image thumbnail

String to image

version (5.67 KB) by Serge
Convert text string to image


Updated 25 Jan 2017

View License

Convert text string to rgb image
str2im - display example text
str2im(str) - text as char array or cellstr
str2im(str,pad) - margin: [N] or [H V] or [L R T B] as pixels or nan
str2im(...,props) - text property as value pairs, doc text
I = str2im(..) - returns rgb image data as uint8
[I,A] = str2im(..) - returns background alpha channel as uint8
[I,A,H,W] = str2im(..) - returns image height and width

-If a margin is nan then the background is cropped up to the text.
-A bit slow because nothing is cached, a figure is generated for each
call and getframe is used, usually take more then 0.03s to run.
-Windows start menu may occlude text if it is not at bottom of screen or
greater then 60 pixels in height.
-Maximum image size is limited by screen resolution: [V_rez-60 H_rez].

Useful text properties:
BackgroundColor - {w} y m c r g b k [r g b]
Color - {k} y m c r g b w [r g b]
FontSize - {20} positive number
FontUnits - {points} pixels normalized inches centimeters
FontWeight - {normal} bold light demi
FontAngle - {normal} italic oblique
FontName - Helvetica FixedWidth ... (see listfonts)
Interpreter - {none} tex latex
HorizontalAlignment - {left} center right

System defaults:
get(0,{'defaultTextFontName' 'FixedWidthFontName'})

figure(1),clf, str2im({'str' '2im'},nan,'back','y','fontw','b','hor','c')
figure(2),clf, str2im('\phi_k^\pi',[0 0 -22 -10],'fonts',40,'interp','t')
figure(3),clf, str2im('$$\int_0^2x^2\sin(x)dx$$','col','b','interp','l')

Example: burn text into image
[T,A,H,W]=str2im(datestr(now),'fontn','FixedWidth','col','y','back',[.2 .2 .2]);

Example: find fixed-width fonts

Example: show font browser gui
s = uicontrol('sty','edi','pos',[ 5 5 150 50],'str',char(reshape(32:127,[],6)'),'max',1000);
n = uicontrol('sty','pop','pos',[155 5 150 20],'str',['FixedWidth';sort(listfonts)]);
w = uicontrol('sty','pop','pos',[305 5 60 20],'str',{'normal' 'bold' 'light' 'demi'});
a = uicontrol('sty','pop','pos',[365 5 60 20],'str',{'normal' 'italic' 'oblique'});
c = uicontrol('sty','pop','pos',[425 5 40 20],'str',{'k' 'y' 'm' 'c' 'r' 'g' 'b' 'w'});
b = uicontrol('sty','pop','pos',[465 5 40 20],'str',{'w' 'y' 'm' 'c' 'r' 'g' 'b' 'k'});
z = uicontrol('sty','pop','pos',[505 5 40 20],'str',cellstr(num2str((1:200)')),'val',20);
F = @(v,i)v{i}; F = @(h)F(get(h,'str'),get(h,'val')); %function to get popup str
set([n w a c b z],'call',@(x,y)str2im(get(s,'str'),'fontn',F(n),'fontw',...

See also: text, getframe, listfonts, rgb2gray, im2double
Serge 2016, for corrections or suggestions email:
Tested on R2010b Win, R2012a Win, more testing feedback is desired

Cite As

Serge (2021). String to image (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (4)


V K, try searching for "OCR", optical character recognition.

v k

I need the exact opposite of this. im2str needed, which can take take an image as an input, and when str=im2str('imageFile.jpg') is called, it plots the image shape.

Arash Rabbani

The idea is grat, Can you make it with higher resolution too?

kirin chain

MATLAB Release Compatibility
Created with R2010b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!