File Exchange

image thumbnail

String to image

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

3 Downloads

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

Remarks:
-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'})

Example:
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]);
figure(4),clf,I=imread('peppers.png');I(11:10+H,11:10+W,:)=I(1:H,1:W,:)+T;imshow(I)

Example: find fixed-width fonts
f=listfonts,f(cellfun(@(f)numel(str2im('A','fontn',f))==numel(str2im('.','fontn',f)),f))

Example: show font browser gui
figure(6),clf,set(gcf,'tool','fig')
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',...
F(w),'fonta',F(a),'color',F(c),'back',F(b),'fonts',str2double(F(z))));

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

Comments and Ratings (2)

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

kirin chain

Updates

1.0.0.0

Minor changes

1.0.0.0

Minor changes

1.0.0.0

Minor changes

1.0.0.0

Fix to keep window on top

1.0.0.0

Minor changes

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