Good Day, Can we convert .txt to .jpg or .png
13 views (last 30 days)
Show older comments
I want to save a text file as image(jpg or png) but the text contains data (some numbers). Is there any function like Imshow to convert from txt to jpg or png?
Thanks in advance
0 Comments
Answers (3)
DGM
on 6 Jan 2022
Edited: DGM
on 6 Jan 2022
Besides insertText(), which is a CVT tool, and direct figure capture, there are other text-image tools on the File Exchange:
MIMT has both textim() and textblock(), which generate compact images of text in legacy hardware fonts. (CP437 based)
text2im() by Tobias Kiessling is similar, but only capable of a single font (the same default font used by MIMT textim()) (also CP437 based)
text2im by Rik offers a handful of modern font faces, though it supports a small charset and requires network connection.https://www.mathworks.com/matlabcentral/fileexchange/75021-text2im
text_to_image by Alec Jacobson is more flexible, but uses Imagemagick (external dependency), and is consequently slower.
There are also others:
And there are slightly different approaches:
Using MIMT textim():
mynumber = 1234;
A = textim(sprintf('my favorite password is %d',mynumber),'ibm-iso-16x9');
imwrite(A,'textpicture.png')
From here, the image can be resized (imresize()), padded (padarray() or MIMT addborder()), combined with other images. For compositing with another image, see this:
19 Comments
Walter Roberson
on 24 Jan 2022
Hmmm... I thought I saw a Mathworks employee say that only a single color table was used. However when I test now
img = zeros(48, 64, 'uint8');
cmaps = [1 0 0; 0 1 0; 0 0 1];
filename = tempname + ".gif"
imwrite(img, cmaps(1,:), filename, 'writemode', 'overwrite', 'loop', inf)
imwrite(img, cmaps(2,:), filename, 'writemode', 'append')
imwrite(img, cmaps(3,:), filename, 'writemode', 'append')
[I, C] = imread(filename, 'frames', 'all');
whos
info = imfinfo(filename)
CT = info.ColorTable
although CT comes out as only suitable for the first frame, when I use my OS programs to examine the image fle, I do clearly see different color of frames, indicating that the local color tables must have been written properly.
DGM
on 25 Jan 2022
Well I guess that's good to know. Since apparently imfinfo() can no longer read any color tables correctly, I didn't have an easy means to check that imwrite() was still working correctly when using the online tools to check the behavior in the newer versions.
Walter Roberson
on 5 Jan 2022
MyText = 'Fly! Little white dove, fly!';
img = repmat(uint8(MyText), 20, 5, 3);
imshow(img)
But perhaps what you mean is more like
img = ones(30, 200, 3, 'uint8');
img = insertText(img, [1 1], MyText, 'TextColor', 'red', 'BoxColor', 'black');
imshow(img)
7 Comments
Walter Roberson
on 9 Jan 2022
image_area_pixels = 1280 * 1024
number_of_numbers = 200 * 200
average_pixels_per_number = image_area_pixels / number_of_numbers
height_ratio = 3/2; %characters are taller than they are wide
syms character_width characters_per_number positive
number_width = character_width * characters_per_number
number_height = character_width * height_ratio
number_area = number_width * number_height
average_character_width = simplify(solve(number_area == average_pixels_per_number))
fplot(average_character_width, [1 10])
xlabel('characters per number')
ylabel('character width, pixels');
So if each number is one character wide, then if your characters are more than 4 pixels wide and 6 pixels tall, you cannot fit 200 x 200 in a single 1280 x 1024 image. If each number. A number that is 4 pixels wide is not readable.
yanqi liu
on 6 Jan 2022
clc; clear all; close all;
figure;
% init edit
etf = uicontrol('Style','edit');
uicontrol(etf);
set(etf,'units','normalized','position',[0,0,1,1],'Max',10,'HorizontalAlignment','left','fontsize',14)
% txt
txt = {' I want to save a text file as image(jpg or png) but the text contains data (some numbers)',...
'Is there any function like Imshow to convert from txt to jpg or png?',...
'Thanks in advance!'};
set(etf, 'String', txt);
% get image
f = getframe(gcf);
f = frame2im(f);
figure('Color','w'); imshow(f, []);
% save image
% imwrite(f,'res.png');
See Also
Categories
Find more on Image Data 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!