postprocess_image_names.py 943 B

12345678910111213141516171819202122232425262728293031323334353637
  1. #!/usr/bin/env python
  2. if __name__ != '__main__': raise Exception("Do not import me!")
  3. import os
  4. import re
  5. from cvargparse import Arg
  6. from os.path import isfile, join, basename, splitext
  7. from tqdm import tqdm
  8. from utils import parser
  9. from utils import imaging
  10. def main(args):
  11. # class_names = imaging.get_classnames(args.classnames)
  12. content = imaging.get_content(args.folder, args.extensions)
  13. for root, fnames in tqdm(content):
  14. folder = basename(root)
  15. if not re.match(r"^\d+\..*$", folder): continue
  16. fnames = sorted(fnames, key=lambda name: int(re.match(r"^(\d+)\..*$", name).group(1)))
  17. for i, fname in enumerate(fnames):
  18. ext = splitext(fname)[1][1:].strip().lower()
  19. if ext == "jpeg":
  20. ext = "jpg"
  21. new_name = f"{folder}_{i:04d}.{ext}"
  22. new_path = join(root, new_name)
  23. assert not isfile(new_path), f"File exists: {new_path}"
  24. os.rename(join(root, fname), join(root, new_name))
  25. main(parser.parse_args([
  26. ])