-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
padder.py
64 lines (54 loc) · 2.01 KB
/
padder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""
This script renames the sequencially numbered files with a new name with padded zeroes.
"""
from tvlib._config import *
def _find_digit_bound(filename: str) -> tuple[bool, int] | tuple[bool, None]:
"""
Find the ending digit of a filename.
If found, return true and the number.
"""
for i in range(1, len(filename)):
i = 0-i
if filename[i].isdigit():
continue
else:
return (True, i)
return (False, None)
def padder(base_path: str):
"""
Traverse through the folders in a top-folder.
"""
if path.isdir(base_path):
for root, folders, __ in walk(base_path):
for folder in folders:
base_path = path.join(root, folder)
if path.isdir(base_path):
_file_traverse(base_path)
else:
print("Not a dir")
def _file_traverse(base_path: str):
"""
Traverse through files in a directory.
If the fil ehas a number to it, pad
"""
if path.isdir(base_path):
for root, _, images in walk(base_path):
for image in images:
frame_path = path.join(root, image)
if path.isfile(frame_path):
debug(path.basename(frame_path)[:-4])
filename = path.basename(frame_path)[:-4]
dirname = path.basename(path.dirname(frame_path))
_padded_rename(filename, dirname, frame_path)
def _padded_rename(filename: str, dirname: str, frame_path: str) -> None:
"""
Attempt to rename a file if its ending number is found.
"""
found, digit = _find_digit_bound(filename)
if found:
new_filename_prefix = filename[:(digit+1)]
new_filename_suffix = filename[(digit+1):].zfill(6) + ".png"
debug(new_filename_prefix + new_filename_suffix)
rename(frame_path, path.join(IMAGE_DIR, dirname, new_filename_prefix + new_filename_suffix))
if __name__ == "__main__":
padder(IMAGE_DIR)