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

2 次查看(过去 30 天)
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.

采纳的回答

Akira Agata
Akira Agata 2018-10-10
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Convert Image Type 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by