Python小工具(1)-按照时间顺序将照片进行重命名
在网站下载了一些照片,需要批量处理,全选后鼠标停留在第一个照片右键重命名0000,之后,在进行以下操作。
import os
from PIL import Image
def rename_images(folder_path):
# 获取所有图像文件及其最后修改时间
images = [(filename, os.path.getmtime(os.path.join(folder_path, filename)))
for filename in os.listdir(folder_path)
if filename.endswith(('.jpg', '.png', '.jpeg'))]
# 按照最后修改时间对文件进行排序
images.sort(key=lambda x: x[1])
# 重命名文件
for i, (filename, _) in enumerate(images):
file_path = os.path.join(folder_path, filename)
with Image.open(file_path) as img:
new_filename = '{}.{}'.format(i+1, img.format.lower())
os.rename(file_path, os.path.join(folder_path, new_filename))
if __name__ == "__main__":
folder_path = r'E:\c++' # 替换为您的文件夹路径
rename_images(folder_path)```
更改上述文件夹路径,安装相应的模块,运行以上程序,就可以按照时间顺序进行排序。