-
Notifications
You must be signed in to change notification settings - Fork 0
/
daily_subset_and_rename.py
executable file
·109 lines (80 loc) · 3.04 KB
/
daily_subset_and_rename.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
'''
daily_subset_and_rename.py
Purpose: copy images from times X to Y for each day to new folder, optionally rename using batch_rename.py
'''
import os
import sys
import glob
import re
import shutil
import batch_rename
def int2padstr(int_val):
"""
:param int_val:
:return:
"""
if int_val < 10:
return str(int_val).zfill(2)
else:
return str(int_val)
def build_regex(int_val, end_val=9):
"""
:param int_val:
:param end_val:
:return:
"""
return '[{0}][{1}-{2}]'.format(int_val[0], int_val[1], end_val)
def build_re_range(val_one, val_two):
# make sure numbers have ones and tens place
str_one = int2padstr(val_one)
str_two = int2padstr(val_two)
# if same tens place, do not create min|max range
if str_one[0] == str_two[0]:
time_range = '{0}'.format(build_regex(str_one, end_val=int(str_two[1])))
else:
time_range = '[{0}|{1}]'.format(build_regex(str_one), build_regex(str_two))
return re.compile(time_range + ':\d{2}:\d{2}')
def main(src, dst, time_start, time_end, ext='.jpg', renumber=False, dryrun=False):
"""
:param dir_in: <str>
:param dir_out: <str>
:param time_start: <int>
:param time_end: <int>
:param ext: <str>
:param renumber: <bool>
:param dryrun: <bool>
:return:
"""
if not os.path.exists(dst) and not dryrun:
print("Creating directory {0}".format(dst))
os.mkdir(dst)
# get files
fn_in = sorted(glob.glob(os.path.join(src, '*' + ext)))
if not fn_in:
sys.exit("Could not find any files for input dir {0} using extension {1}".format(src, ext))
# sort files by time of day
fn_in_match = [i for i in fn_in if build_re_range(time_start, time_end).findall(i)]
if not fn_in_match:
sys.exit("Could not find any matches in {0} between hours {1} and {2}".format(src, time_start, time_end))
# copy to new dir
if not dryrun:
[shutil.copy2(fc, dst) for fc in fn_in_match]
# optionally re-number using batch_rename.py
if renumber:
batch_rename.main(dst, renumber=True)
else:
print("--dryrun uesd; no files will be moved. Results: {0}".format(fn_in_match))
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
req_named = parser.add_argument_group("Required named arguments")
req_named.add_argument("src", help="Dir of input images.")
req_named.add_argument("dst", help="Output dir for moved images.")
req_named.add_argument("time_start", help="Start hour.")
req_named.add_argument("time_end", help="End hour.")
parser.add_argument("--ext", help="Extension of input files (default='.jpg')", default=".jpg", required=False)
parser.add_argument("--renumber", help="Rename files to sequential numbers by alphanumeric order",
action="store_true", required=False)
parser.add_argument("--dryrun", help="Run script, but do not execute actions", action="store_true", required=False)
arguments = parser.parse_args()
main(**vars(arguments))