-
Notifications
You must be signed in to change notification settings - Fork 0
/
trick.py
60 lines (52 loc) · 1.8 KB
/
trick.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
56
57
58
59
60
import os
import re
import cv2 as cv
def imgs_name_add(imgRoot, cnt):
"""
imgRoot:文件夹名字
cnt:名字以多少号序号开始
"""
imgNameList = []
for root, dirs, files in os.walk(imgRoot, topdown=True):
for name in files:
_, ending = os.path.splitext(name)
if ending == '.jpg':
imgNameList.append(os.path.join(name))
print(imgNameList)
# 正则取出前缀排序
print(imgNameList[0])
x = re.findall(r"\d+", imgNameList[0])
print(x)
imgNameList.sort(key=lambda x: int(re.findall(r"\d+", x)[0]))
# 按照顺序修改名字
for i in range(len(imgNameList) - 1, -1, -1):
oldName = os.path.join(imgRoot, imgNameList[i])
newName = os.path.join(imgRoot, str(i + cnt).zfill(4) + '.jpg')
print(oldName, '->', newName)
os.rename(oldName, newName)
print('name sorting completed')
def imgs_name_del(imgRoot, cnt):
"""
imgRoot:文件夹名字
cnt:名字以多少号序号开始
"""
imgNameList = []
for root, dirs, files in os.walk(imgRoot, topdown=True):
for name in files:
_, ending = os.path.splitext(name)
if ending == '.jpg':
imgNameList.append(os.path.join(name))
print(imgNameList)
# 正则取出前缀排序
print(imgNameList[0])
x = re.findall(r"\d+", imgNameList[0])
print(x)
imgNameList.sort(key=lambda x: int(re.findall(r"\d+", x)[0]))
# 按照顺序修改名字
for i in range(0, len(imgNameList), 1):
oldName = os.path.join(imgRoot, imgNameList[i])
newName = os.path.join(imgRoot, str(i+cnt).zfill(4) + '.jpg')
print(oldName, '->', newName)
os.rename(oldName, newName)
print('name sorting completed')
# imgs_name_add('PC_test/', 188)