Good Day, Can we convert .txt to .jpg or .png

13 views (last 30 days)
Rayan Shaya
Rayan Shaya on 5 Jan 2022
Commented: DGM on 25 Jan 2022
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

Answers (3)

DGM
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
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
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.

Sign in to comment.


Walter Roberson
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
Walter Roberson on 9 Jan 2022
image_area_pixels = 1280 * 1024
image_area_pixels = 1310720
number_of_numbers = 200 * 200
number_of_numbers = 40000
average_pixels_per_number = image_area_pixels / number_of_numbers
average_pixels_per_number = 32.7680
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_width = 
number_height = character_width * height_ratio
number_height = 
number_area = number_width * number_height
number_area = 
average_character_width = simplify(solve(number_area == average_pixels_per_number))
average_character_width = 
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.
Rayan Shaya
Rayan Shaya on 11 Jan 2022
Thank you again guys for your time.
Answering DGM :
1) No specific constraint on the geometry, but I want to use all the matlab figure space if it can be done so it would be more effecient.
2) One row per line ( Take out all the spaces between columns and put them as a line)
That's why Im trying to read from my text file and use insertText() to write every line till I finish all the rows of my text file (using for loop) but I am not able to use insertText function correctly.
3) Integer and one digit (either 1 2 3 4 5...)
4) Doesn't matter incase it is still readable
5) I might resize it if I find it large enough, I can rescale it make it smaller
Answering Walter:
I have a question can't we know how many characters (digits in my case) every line can fit and how many rows; Like can I get the accepted hight and width of the figure using how many lines and how many carachters inside the lines
Hight*Width
how many lines (rows)* how many character in every line
I hope I made the question clear. I would really appretiate it if you can help and if not I'm really thankful that you helped me before.

Sign in to comment.


yanqi liu
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');
  1 Comment
Rayan Shaya
Rayan Shaya on 6 Jan 2022
Thank you for your time Yanqi, but my .txt file is a matrix (200*200) full of numbers so if I copy and paste them as a text in MyText='....' the code will crash. I have the results as a txt file inside matlab so I want to see if I directly convert it to an image, if not I can copy and paste the results outside matlab and get the image form. But all what I want is to get it directly from matlab.
Thank you again

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!