CONVERT TEXT TO IMAGE
33 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Image Analyst
2014-12-24
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 个评论
更多回答(2 个)
Sean de Wolski
2014-12-24
编辑:Sean de Wolski
2014-12-24
>> imshow(insertText(zeros(200,700),[100 100],'Hello World','BoxOpacity',0,'FontSize',50,'TextColor','r'))
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/172612/image.png)
Computer Vision System Toolbox required.
4 个评论
Wenjie Wu
2024-3-28
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
2024-3-29
Do what it says "Contact your License Administrator to review the Options File." or else call tech support for installation help.
DGM
2024-3-29
编辑:DGM
2024-3-29
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');
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1654901/image.png)
The most compact hexadecimal-only human-readable font has 5x4px characters.
mytext = '0123456789 ABCDEF';
textpict = textim(mytext,'micronum');
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1654906/image.png)
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 个评论
DGM
2024-3-29
编辑:DGM
2024-3-29
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')
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1654936/image.png)
... 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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Convert Image Type 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!