suffix.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. '''
  2. Author: zeke-chin zeke-chin@icloud.com
  3. Date: 2022-10-11 16:38:18
  4. LastEditors: zeke-chin zeke-chin@icloud.com
  5. LastEditTime: 2022-10-12 17:53:30
  6. FilePath: /test_script/HR_OCR/tools/suffix.py
  7. Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
  8. '''
  9. import os
  10. from pathlib import Path
  11. from itertools import chain
  12. import sys
  13. # conf
  14. target_path = '/Users/zeke/work/sx/OCR/image_data/营业执照90'
  15. #suffix = sys.argv[2]
  16. suffix = 'jpg' if len(sys.argv) != 3 else sys.argv[2]
  17. def get_range(n):
  18. len_n = len(str(n))
  19. for num in range(1, n + 1):
  20. output = str(num)
  21. while len(output) < len_n:
  22. output = f'0{output}'
  23. yield output
  24. file_paths = list(chain(*[Path(target_path).rglob(f'*.{ext}') for ext in ['jpeg', 'jpg', 'png', 'JPG', 'PNG']]))
  25. print(len(file_paths))
  26. num = len(file_paths)
  27. file_name_list = list(get_range(num))
  28. for i in range(num):
  29. file = file_paths[i]
  30. print(file)
  31. new = file.parent / f'{file_name_list[i]}_img.{suffix}'
  32. file.rename(new)