Read and save multiple PNG images after inserting text on them (This text is the filename itself without any extensions)

1 view (last 30 days)
I have around 400 PNG images on which I need to insert text. The text that I want to insert is the filename itself without any extension. After that, I want to save those files in a different folder.
The code for writing text on the file is:
I = imread('IRC338H_DTIFB01_pre_ACC.png');
text_str = ['IRC338H_DTIFB01_pre_ACC'];
position = [10,10];
Filename = insertText(I, position, text_str,'Fontsize',18,'TextColor','white');
figure
imshow(Filename)
I wanted help with forming the loop so that all the images in the folder are read, text is inserted and then the images are saved sequentially.
The format of file names is IRC338H_DTIFB01_pre_ACC.png. Where the characters that change are DTIFB01, DTIFB02, and so on. Similarly it the filename either ends with ACC or M1.
Just to make it clear, the file names are in the format:
IRC338H_DTIFB01_pre_ACC; IRC338H_DTIFB01_pre_M1; IRC338H_DTIFB02_pre_ACC; IRC338H_DTIFB02_pre_M1;
and so on.

Accepted Answer

Akira Agata
Akira Agata on 10 Oct 2018
Assuming your original .png files are stored in 'folder1', the following example can generates images with file name and saves them to 'folder2'.
fileList = dir(fullfile(pwd,'folder1','*.png'));
position = [10,10];
for kk = 1:numel(fileList)
I = imread(fullfile(fileList(kk).folder,fileList(kk).name));
[~,fileName] = fileparts(fileList(kk).name);
I2 = insertText(I,position,fileName,'Fontsize',18,'TextColor','white');
imwrite(I2,fullfile(pwd,'folder2',fileList(kk).name));
end

More Answers (0)

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!