CONVERT TEXT TO IMAGE

88 views (last 30 days)
muath shaikh
muath shaikh on 24 Dec 2014
Edited: DGM on 29 Mar 2024 at 3:51
Dear Colleagues, I wanna convert text into small image JPG image,like Name :'Roburt'; But i wanna to be smallest image like 2 * 7 for example, no need to be large

Accepted Answer

Image Analyst
Image Analyst on 24 Dec 2014
If you don't have the Computer Vision System Toolbox you can use text() and then save the axes with export_fig (Available from the File Exchange).
  2 Comments
muath shaikh
muath shaikh on 24 Dec 2014
Actually i dont have Computer vision system toolbox Could you explain your idea by example, and how to save as image
Image Analyst
Image Analyst on 24 Dec 2014
text(x,y, 'Hello World');
export_fig(filename, gca);

Sign in to comment.

More Answers (2)

Sean de Wolski
Sean de Wolski on 24 Dec 2014
Edited: Sean de Wolski on 24 Dec 2014
>> imshow(insertText(zeros(200,700),[100 100],'Hello World','BoxOpacity',0,'FontSize',50,'TextColor','r'))
Computer Vision System Toolbox required.
  4 Comments
Wenjie Wu
Wenjie Wu on 28 Mar 2024 at 21:52
Hello,
I have the 'Computer Vision System Toolbox' installed/licensed, on 2023b.
But when I run the insertText function, it returns:
License checkout failed.
License Manager Error -39
User/host not on INCLUDE list for Video_and_Image_Blockset.
Contact your License Administrator to review the Options File.
Troubleshoot this issue by visiting:
https://www.mathworks.com/support/lme/39
Diagnostic Information:
Feature: Video_and_Image_Blockset
License path: xxx
Licensing error: -39,147.
Image Analyst
Image Analyst on 29 Mar 2024 at 3:29
Do what it says "Contact your License Administrator to review the Options File." or else call tech support for installation help.

Sign in to comment.


DGM
DGM on 29 Mar 2024 at 2:39
Edited: DGM on 29 Mar 2024 at 3:11
Old question, but let's approach this from the specifics of the original use case. We want a small image of some text. Trying to use CVT insertText() or figure capture are going to both be severely limited in creating compact text images. MIMT textim() can generate images of text, and is generally built around a focus on compactness. All fonts are bitmapped and fixed-width.
The most compact alphanumeric font has 8x5px characters.
mytext = 'robbybobbybertyboi';
textpict = textim(mytext,'everex-me');
The most compact hexadecimal-only human-readable font has 5x4px characters.
mytext = '0123456789 ABCDEF';
textpict = textim(mytext,'micronum');
Bear in mind that those images have one pixel of padding on two edges, so you technically could trim those off as well. MIMT textim() also has convenient 2x2 hexadecimal-only fonts, but they're not intended to be human-readable. Those are the sacrifices made for compactness.
That aside, you're not going to find any fonts in any tools that will allow you to fit 6 human-readable letters into a 2px x 7px image.
If your goal is to create images with small features like this, then using JPG is unacceptable. Do not use JPG unless you like ruining your images and making them take up more disk space for no good reason.
For other text-to-image needs, see:
... at least that's what I have in my notes.
  1 Comment
DGM
DGM on 29 Mar 2024 at 3:08
Edited: DGM on 29 Mar 2024 at 3:51
FWIW, this is what you get when you try to use AA scalable fonts and lossy workflows for small text images.
mytext = 'robbybobbybertyboi';
sz = [8 90]; % try to cram the text into the same space used by textim()
mybg = zeros(sz);
textpict = insertText(mybg,fliplr(sz/2),mytext,'anchorpoint','center', ...
'BoxOpacity',0,'FontSize',8,'TextColor','w');
% now let's save it as a JPG and read it back
imwrite(textpict,'trash.jpg')
textpict = imread('trash.jpg');
% the image is too small to actually see on the forum,
% so let's scale it for sake of inspection
textpict = imresize(textpict,10,'nearest');
imshow(textpict,'border','tight')
Yeah, that's trash.
% use figure capture
mytext = 'robbybobbybertyboi';
ht = text(0,0,mytext);
ht.FontSize = 5; % try to replicate the same image size as textim()
ht.VerticalAlignment = 'bottom';
ht.Units = 'pixels';
hax = gca;
hax.Units = 'pixels';
hax.Position(3:4) = ht.Extent(3:4);
textpict = export_fig(gca); % capture the axes
textpict = 255-textpict; % invert it so it matches other examples
% now let's save it as a JPG and read it back
imwrite(textpict,'trash.jpg')
textpict = imread('trash.jpg');
% the image is too small to actually see on the forum,
% so let's scale it for sake of inspection
textpict = imresize(textpict,10,'nearest');
imshow(textpict,'border','tight')
... and that's even worse.
For the given task, neither are nearly as simple to use, especially if you expect to be able to get consistently predictable image sizes.

Sign in to comment.

Categories

Find more on Convert Image Type in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!