forked from HunterCQu/AITools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rename_img_list_encode.py
55 lines (45 loc) · 1.74 KB
/
rename_img_list_encode.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# -*- coding: utf-8 -*-
# Author : Andy Liu
# Last modified: 2021-01-20
import os
import shutil
import sys
import argparse
import random
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('--srcImgPath', type=str, help='src image directory')
parser.add_argument('--dstImgPath', type=str, default='./rename_img_encode' , help='dst image directory')
parser.add_argument('--matchedListPath', type=str, default='./match_list.txt' , help='matched list file path')
args = parser.parse_args()
return args
def rename(srcPath, dstPath, matchedListPath):
src_list = []
dst_list = []
srcList = os.listdir(srcPath)
srcList = srcList.sort()
outputFile = open(matchedListPath,'w+')
for i, f in enumerate(srcList):
fileInfo = f.split(".")
srcFilePath = os.path.join(srcPath, f)
dstFilePath = os.path.join(dstPath, str(i)+"."+fileInfo[-1])
shutil.copyfile(srcFilePath, dstFilePath)
outputFile.write(f"{i},{f}\n")
outputFile.close()
if __name__ == '__main__':
args = parse_args()
srcImgPath = args.srcImgPath
dstImgPath = args.dstImgPath
matchedListPath = args.matchedListPath
if not os.path.exists(srcImgPath):
print("Error !!! %s is not exists, please check the parameter"%srcImgPath)
sys.exit(0)
if not os.path.exists(dstImgPath):
os.makedirs(dstImgPath)
print("Warning !!! %s is not exists, using the default path: ./rename_img_encode"%dstImgPath)
# sys.exit(0)
if not os.path.exists(matchedListPath):
print("Warning !!! %s is not exists, using the default path: ./match_list.txt"%matchedListPath)
# sys.exit(0)
rename(srcImgPath, dstImgPath, matchedListPath)
print("Done!")