Rename jpg files in a folder
조회 수: 12 (최근 30일)
이전 댓글 표시
I need some help here,
So I have like 200 Images with the name of 1 2 3 4 ... n
Ineed the file to be renamed as 001 002 .... 012... 034 .... 180 ... 200
How to do that?
Thanks,
채택된 답변
Image Analyst
2020년 10월 23일
Try this:
folder = pwd; % Wherever your input images are stored.
outputFolder = fullfile(folder, '/renamed'); % Can be the same as the input folder if you want.
% Make output folder if it exists.
if ~isfolder(outputFolder)
mkdir(outputFolder)
end
fileList = dir('*.bmp')
% Loop over all files.
for k = 1 : length(fileList)
% Get this filename.
thisFileName = fullfile(fileList(k).folder, fileList(k).name);
% Find out just the base filename without the folder.
[~, baseFileNameNoExt, ext] = fileparts(thisFileName);
number = str2double(baseFileNameNoExt);
% Skip non-numbers
if isnan(number)
continue;
end
fprintf('Processing %s...\n', thisFileName);
% Optionally display the image
% % Read in the input image.
% originalImage = imread(thisFileName);
% % Display it.
% imshow(originalImage);
% drawnow;
% Create the new base filename without the folder.
baseOutputFileName = sprintf('%3.3d%s', number, ext);
% Create output image filename....
fprintf(' Renaming to %s to %s...\n', baseFileNameNoExt, baseOutputFileName);
% Create the output filename which will be in the output folder.
outputFileName = fullfile(outputFolder, baseOutputFileName);
movefile(thisFileName, outputFileName); % or imwrite()
end
message = sprintf('Done processing %d images', length(fileList))
uiwait(helpdlg(message));
If you want to save the renamed files in a different folder, use imwrite(), otherwise if you want to rename in the same folder use movefile().
댓글 수: 4
Niladri
2023년 3월 7일
이동: Voss
2023년 3월 7일
How to automatically change the name of several IMAGE files (in .bmp) stored in a folder?
Folder name is: gauss and filenames are stored as: GAUSS0, GAUSS1, GAUSS2,......etc.
I want to make them: GAUSS501, GAUSS502, GAUSS503,.... etc. in the same/different folder.
A quick help will be appreciated!
Thank you.
Niladri
Image Analyst
2023년 3월 7일
이동: Voss
2023년 3월 7일
@Niladri see code below and make appropriate changes.
% Optional initialization steps
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
folder = pwd; % or 'C:\Users\niladri\pictures'
if ~isfolder(folder)
msgboxw('Warning: folder does not exist.\n"%s"\n\nPlease pick a new folder.', folder);
folder = uigetdir;
if ~isfolder(folder)
return;
end
end
filePattern = fullfile(folder, 'GAU*.bmp');
fileList = dir(filePattern)
if isempty(fileList)
msgboxw('Warning: No .BMP files found in:\n"%s"\n\nPlease pick a new folder.', folder);
folder = uigetdir;
if ~isfolder(folder)
return;
end
end
allFileNames = {fileList.name}
for k = 1 : numel(allFileNames)
baseFileName = allFileNames{k};
thisFileName = fullfile(folder, baseFileName);
fprintf('Checking "%s".\n', thisFileName)
% Change fro GAUSS1 to GAUSS501 by replacing the first 5 characters
newBaseFileName = strrep(baseFileName, 'GAUSS', 'GAUSS50');
outputFullFileName = fullfile(folder, newBaseFileName);
% Rename the .xlsx
fprintf(' Renaming "%s" to "%s"\n', baseFileName, newBaseFileName);
movefile(thisFileName, outputFullFileName);
end
if ispc
winopen(folder);
end
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Image Processing and Computer Vision에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!