56 lines
1.8 KiB
Python
56 lines
1.8 KiB
Python
import os
|
|
from pathlib import Path
|
|
|
|
def rename_images_in_folder(folder_path):
|
|
# 支持的图片扩展名(不区分大小写)
|
|
image_extensions = {'.jpg', '.jpeg', '.png', '.bmp', '.gif', '.tiff', '.webp'}
|
|
|
|
# 转换为 Path 对象
|
|
folder = Path(folder_path)
|
|
|
|
# 检查文件夹是否存在
|
|
if not folder.exists():
|
|
print(f"❌ 文件夹不存在: {folder_path}")
|
|
return
|
|
|
|
if not folder.is_dir():
|
|
print(f"❌ 路径不是文件夹: {folder_path}")
|
|
return
|
|
|
|
# 获取所有图片文件
|
|
image_files = [f for f in folder.iterdir()
|
|
if f.is_file() and f.suffix.lower() in image_extensions]
|
|
|
|
if not image_files:
|
|
print("🔍 文件夹中没有找到图片文件。")
|
|
return
|
|
|
|
# 排序(按文件名排序,确保顺序一致)
|
|
image_files.sort()
|
|
|
|
print(f"📁 正在处理文件夹: {folder}")
|
|
print(f"🖼️ 找到 {len(image_files)} 个图片文件")
|
|
|
|
renamed_count = 0
|
|
for idx, file_path in enumerate(image_files, start=1):
|
|
new_name = f"{idx}.jpg" # 统一输出为 .jpg 格式
|
|
new_path = folder / new_name
|
|
|
|
# 防止覆盖已存在的目标文件
|
|
while new_path.exists():
|
|
print(f"⚠️ {new_name} 已存在,跳过或改名?")
|
|
# 可以选择跳过,或用不同逻辑处理
|
|
break
|
|
else:
|
|
file_path.rename(new_path)
|
|
print(f"✅ {file_path.name} → {new_name}")
|
|
renamed_count += 1
|
|
|
|
print(f"\n✅ 完成!共重命名 {renamed_count} 个文件。")
|
|
|
|
# ===========================
|
|
# 🔧 使用这里:设置你的文件夹路径
|
|
# ===========================
|
|
if __name__ == "__main__":
|
|
folder = r"/home/hx/下载/2025-09-24" # <-- 修改为你的图片文件夹路径
|
|
rename_images_in_folder(folder) |