Windows下對資料夾下所有圖片批量重新命名(附C++,python,matlab程式碼)
阿新 • • 發佈:2019-01-24
原資料夾
重新命名之後
C++
#include <iostream>
#include <io.h> //對系統檔案進行操作的標頭檔案
#include <string>
#include <sstream>
#include<vector>
using namespace std;
const int N = 6; //整型格式化輸出為字串後的長度,例如,N=6,則整型轉為長度為6的字串,12轉為為000012
const string FileType = ".jpg"; // 需要查詢的檔案型別
/* 函式說明 整型轉固定格式的字串
輸入:
n 需要輸出的字串長度
i 需要結構化的整型
輸出:
返回轉化後的字串
*/
string int2string(int n, int i)
{
char s[BUFSIZ];
sprintf(s, "%d", i);
int l = strlen(s); // 整型的位數
if (l > n)
{
cout << "整型的長度大於需要格式化的字串長度!";
}
else
{
stringstream M_num;
for (int i = 0;i < n - l;i++)
M_num << "0";
M_num << i;
return M_num.str();
}
}
int main()
{
_finddata_t c_file; // 查詢檔案的類
string File_Directory ="E:\\image"; //資料夾目錄
string buffer = File_Directory + "\\*" + FileType;
//long hFile; //win7系統,_findnext()返回型別可以是long型
intptr_t hFile; //win10系統 ,_findnext()返回型別為intptr_t ,不能是long型
hFile = _findfirst(buffer.c_str(), &c_file); //找第一個檔案
if (hFile == -1L) // 檢查資料夾目錄下存在需要查詢的檔案
printf("No %s files in current directory!\n", FileType);
else
{
printf("Listing of files:\n");
int i = 0;
string newfullFilePath;
string oldfullFilePath;
string str_name;
do
{
oldfullFilePath.clear();
newfullFilePath.clear();
str_name.clear();
//舊名字
oldfullFilePath = File_Directory + "\\" + c_file.name;
//新名字
++i;
str_name = int2string(N, i); //整型轉字串
newfullFilePath = File_Directory + "\\"+ str_name + FileType;
/*重新命名函式rename(const char* _OldFileName,const char* _NewFileName)
第一個引數為舊檔案路徑,第二個引數為新檔案路徑*/
int c = rename(oldfullFilePath.c_str(), newfullFilePath.c_str());
if (c == 0)
puts("File successfully renamed");
else
perror("Error renaming file");
} while (_findnext(hFile, &c_file) == 0); //如果找到下個檔案的名字成功的話就返回0,否則返回-1
_findclose(hFile);
}
return 0;
}
將整型格式化輸出到字串中,我是使用了stringstream類,寫了一個函式實現這個功能,還有一種實現方式是,使用sprintf將數字轉為字串,同時可以格式化字串。如下:
char s[50];
int i = 1;
sprintf(s,”%06d”, i); // 將整型i轉為字串s,指定寬度為6,不足的左邊補0
/* 函式說明 整型轉固定格式的字串
輸入:
n 需要輸出的字串長度
i 需要結構化的整型
輸出:
返回轉化後的字串
*/
string int2string(int n, int i)
{
char s[BUFSIZ];
sprintf(s, "%d", i);
int l = strlen(s); // 整型的位數
if (l > n)
{
cout << "整型的長度大於需要格式化的字串長度!";
}
else
{
stringstream M_num;
for (int i = 0;i < n - l;i++)
M_num << "0";
M_num << i;
return M_num.str();
}
}
python
import os
path = "E:\\image"
filelist = os.listdir(path) #該資料夾下所有的檔案(包括資料夾)
count=0
for file in filelist:
print(file)
for file in filelist: #遍歷所有檔案
Olddir=os.path.join(path,file) #原來的檔案路徑
if os.path.isdir(Olddir): #如果是資料夾則跳過
continue
filename=os.path.splitext(file)[0] #檔名
filetype=os.path.splitext(file)[1] #副檔名
Newdir=os.path.join(path,str(count).zfill(6)+filetype) #用字串函式zfill 以0補全所需位數
os.rename(Olddir,Newdir)#重新命名
count+=1
matlab
%%
%圖片儲存路徑為:
%E:\image\car
%E:\image\person
%car和person是儲存車和行人的資料夾
%這些資料夾還可以有多個,
%放在image資料夾裡就行
%該程式碼的作用是將圖片名字改成000123.jpg這種形式
%%
clc;
clear;
maindir='E:\image\';
name_long=6; %圖片名字的長度,如000123.jpg為6,最多9位,可修改
num_begin=1; %影象命名開始的數字如000123.jpg開始的話就是123
subdir = dir(maindir);
n=1;
for i = 1:length(subdir)
if ~strcmp(subdir(i).name ,'.') && ~strcmp(subdir(i).name,'..')
subsubdir = dir(strcat(maindir,subdir(i).name));
for j=1:length(subsubdir)
if ~strcmp(subsubdir(j).name ,'.') && ~strcmp(subsubdir(j).name,'..')
img=imread([maindir,subdir(i).name,'\',subsubdir(j).name]);
imshow(img);
str=num2str(num_begin,'%09d');
newname=strcat(str,'.jpg');
newname=newname(end-(name_long+3):end);
system(['rename ' [maindir,subdir(i).name,'\',subsubdir(j).name] ' ' newname]);
num_begin=num_begin+1;
fprintf('當前處理資料夾%s',subdir(i).name);
fprintf('已經處理%d張圖片\n',n);
n=n+1;
pause(0.1);%可以將暫停去掉
end
end
end
end