-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrename.py
35 lines (25 loc) · 1.04 KB
/
rename.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
import os
import random
import string
IMG_URL = "https://raw.githubusercontent.com/Yiteng-Peng/imgs-hosting/main/"
def is_valid_hex(name):
return len(name) == 12 and all(c in string.hexdigits for c in name)
def generate_random_hex_name():
return ''.join(random.choices(string.hexdigits.lower(), k=12))
def rename_images():
image_extensions = ['.jpg', '.jpeg', '.png', '.gif', '.svg'] # 可根据需要添加其他图片格式的扩展名
new_name_list = []
for file in os.listdir('.'):
if os.path.isfile(file) and os.path.splitext(file)[1].lower() in image_extensions:
name, ext = os.path.splitext(file)
if not is_valid_hex(name):
new_name = generate_random_hex_name() + ext
new_name_list.append(new_name)
os.rename(file, new_name)
print(f'Renamed {file} to {new_name}')
return new_name_list
new_name_list = rename_images()
for i in new_name_list:
print(IMG_URL+i)
if len(new_name_list) == 0:
print('No name is invalid.')