46 lines
1.1 KiB
Python
46 lines
1.1 KiB
Python
# -*- coding:utf8 -*-
|
|
|
|
import os
|
|
import re
|
|
import shutil
|
|
from pathlib2 import Path
|
|
|
|
import string
|
|
import random
|
|
|
|
# 批量命名图片
|
|
def renamePicWithRandomName(srcImgDir):
|
|
for item in srcImgDir.rglob("*.jpg"):
|
|
# 获取图片名
|
|
imgName = item.name
|
|
# 生成随机字符串
|
|
random_str = ''.join(random.sample(string.ascii_letters + string.digits, 32))
|
|
# 生成随机数字
|
|
random_int = str(random.randint(0, 10000))
|
|
# 生成新的图片名
|
|
newName = random_str + random_int + ".jpg"
|
|
# 重命名
|
|
print(f"prepare to rename {imgName} to {newName}")
|
|
item.rename(newName)
|
|
|
|
|
|
|
|
# 批量命名图片
|
|
def renamePic(srcImgDir):
|
|
i=0
|
|
for item in srcImgDir.rglob("*.jpg"):
|
|
# 获取图片名
|
|
imgName = item.name
|
|
newName = str(i)+".jpg"
|
|
i=i+1
|
|
# 重命名
|
|
print(f"prepare to rename {imgName}")
|
|
item.rename(newName)
|
|
|
|
if __name__ == '__main__':
|
|
|
|
# 文件路径--跟代码同目录
|
|
srcImgPath = Path("./")
|
|
renamePicWithRandomName(srcImgPath)
|
|
renamePic(srcImgPath)
|
|
|