diff --git a/renamePhotoBatch.py b/renamePhotoBatch.py index 128a3a1..64df7e0 100644 --- a/renamePhotoBatch.py +++ b/renamePhotoBatch.py @@ -6,6 +6,8 @@ import os import glob import exifread +import os.path, time + # 获取照片的拍摄时间 def get_photo_taken_time(image_path): @@ -17,25 +19,38 @@ def get_photo_taken_time(image_path): else: return None +def get_photo_update_time(image_path): + with open(image_path, 'rb') as f: + return time.ctime(os.stat(image_path).st_mtime) def solution(): # 获取当前时间并格式化为年月日时分秒毫秒 # 设置照片所在的文件夹路径 - folder_path = "C:\\Users\\hyy\\Pictures\\图片" + # folder_path = "C:\\Users\\hyy\\Pictures\\图片" + folder_path = "C:\\Users\\hyy\\Pictures\\轩辕龙儿的文件收集20240803" # 获取文件夹中的所有照片文件 - photo_files = glob.glob(os.path.join(folder_path, "*")) + # photo_files = glob.glob(os.path.join(folder_path, "*")) + photo_files = os.listdir(folder_path) # 遍历照片文件并重命名 for photo in photo_files: - creation_time_stamp = get_photo_taken_time(photo) + creation_time_stamp = get_photo_taken_time(folder_path+"\\"+photo) if creation_time_stamp is None: continue + if photo.startswith('IMG'): + continue formatted_time = "IMG_" + creation_time_stamp.values.replace(':', '').replace(' ', '_') file_name, file_ext = os.path.splitext(photo) + if file_name.startswith('.'): + file_name, file_ext = os.path.splitext(file_name) new_file_name = f"{formatted_time}{file_ext}" new_file_path = os.path.join(folder_path, new_file_name) + index = 1 + while os.path.exists(new_file_path): + new_file_name = f"{formatted_time}-{index}{file_ext}" + new_file_path = os.path.join(folder_path, new_file_name) os.rename(photo, new_file_path) print("照片重命名完成!")