91 lines
2.4 KiB
Python
91 lines
2.4 KiB
Python
|
|
import argparse
|
||
|
|
from pathlib import Path
|
||
|
|
from datetime import datetime
|
||
|
|
|
||
|
|
|
||
|
|
def main():
|
||
|
|
parser = argparse.ArgumentParser(
|
||
|
|
description="Renomeia imagens usando a data de modificação no formato ddMMyyyy_HHmmss.ext"
|
||
|
|
)
|
||
|
|
parser.add_argument(
|
||
|
|
"--images_dir",
|
||
|
|
type=str,
|
||
|
|
required=True,
|
||
|
|
help="Pasta com as imagens (sem recursão). Ex: dataset/new_images",
|
||
|
|
)
|
||
|
|
parser.add_argument(
|
||
|
|
"--force_jpeg",
|
||
|
|
action="store_true",
|
||
|
|
help="Se setado, força a extensão .jpeg em todos os arquivos.",
|
||
|
|
)
|
||
|
|
parser.add_argument(
|
||
|
|
"--dry_run",
|
||
|
|
action="store_true",
|
||
|
|
help="Se setado, só mostra o que faria, sem renomear nada.",
|
||
|
|
)
|
||
|
|
|
||
|
|
args = parser.parse_args()
|
||
|
|
|
||
|
|
images_dir = Path(args.images_dir)
|
||
|
|
if not images_dir.is_dir():
|
||
|
|
raise SystemExit(f"Pasta não encontrada: {images_dir}")
|
||
|
|
|
||
|
|
exts = {".jpg", ".jpeg", ".png", ".bmp", ".tif", ".tiff"}
|
||
|
|
|
||
|
|
files = [
|
||
|
|
p for p in sorted(images_dir.iterdir())
|
||
|
|
if p.is_file() and p.suffix.lower() in exts
|
||
|
|
]
|
||
|
|
|
||
|
|
if not files:
|
||
|
|
print("[INFO] Nenhuma imagem encontrada na pasta.")
|
||
|
|
return
|
||
|
|
|
||
|
|
print(f"[INFO] Encontrados {len(files)} arquivos de imagem em {images_dir}")
|
||
|
|
|
||
|
|
# Pra evitar conflitos, vamos ir gerando nomes únicos
|
||
|
|
used_names = set()
|
||
|
|
|
||
|
|
for src in files:
|
||
|
|
stat = src.stat()
|
||
|
|
mtime = stat.st_mtime
|
||
|
|
dt = datetime.fromtimestamp(mtime)
|
||
|
|
|
||
|
|
base = dt.strftime("%d%m%Y_%H%M%S")
|
||
|
|
|
||
|
|
if args.force_jpeg:
|
||
|
|
target_ext = ".jpeg"
|
||
|
|
else:
|
||
|
|
target_ext = src.suffix.lower()
|
||
|
|
|
||
|
|
# Nome base desejado
|
||
|
|
new_name = f"{base}{target_ext}"
|
||
|
|
dst = images_dir / new_name
|
||
|
|
|
||
|
|
# Se já existe (ou já usamos esse nome pra outro arquivo), adiciona sufixo _01, _02, ...
|
||
|
|
counter = 1
|
||
|
|
while dst.exists() or dst.name in used_names:
|
||
|
|
new_name = f"{base}_{counter:02d}{target_ext}"
|
||
|
|
dst = images_dir / new_name
|
||
|
|
counter += 1
|
||
|
|
|
||
|
|
used_names.add(dst.name)
|
||
|
|
|
||
|
|
if src == dst:
|
||
|
|
# já está com o nome correto
|
||
|
|
continue
|
||
|
|
|
||
|
|
print(f"{src.name} -> {dst.name}")
|
||
|
|
|
||
|
|
if not args.dry_run:
|
||
|
|
dst.parent.mkdir(parents=True, exist_ok=True)
|
||
|
|
src.rename(dst)
|
||
|
|
|
||
|
|
if args.dry_run:
|
||
|
|
print("\n[INFO] DRY RUN: nada foi renomeado de verdade.")
|
||
|
|
else:
|
||
|
|
print("\n[OK] Renomeação concluída.")
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
main()
|