-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_script.py
executable file
·110 lines (82 loc) · 2.92 KB
/
generate_script.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
110
#!/usr/bin/env python
import argparse
from os.path import commonprefix
from pathlib import Path
from random import choice
from textwrap import dedent
def save_script(name: str, script: str) -> None:
script_path = Path(__file__).parent / 'scripts' / f'{name}.py'
script_path.write_text(script)
print(f'Saved to {script_path}')
def single(path: str) -> None:
"""Simple single shot movie"""
resolved_path = Path(path).resolve()
sorted_images = sorted(image_path for image_path in resolved_path.iterdir() if image_path.is_file())
first = sorted_images[0].stem
last = sorted_images[-1].stem
poster = choice(sorted_images)
fps = 24
deflicker = 3
script = dedent(
f"""
from pathlib import Path
from time_lapse import make_movie, thumbnail
NAME = Path(__file__).stem
PATTERN = '{path}/*.tif' # {first} - {last}
POSTER = '{poster}'
if __name__ == '__main__':
make_movie(NAME, PATTERN, {fps}, {deflicker}, watermark=True, verbose=False, dryrun=False)
thumbnail.create_thumbnail(NAME, Path(POSTER))
""",
).lstrip()
save_script(resolved_path.name, script)
def multiple(*paths: str) -> None:
"""Simple multiple shots stitched together"""
resolved_paths = [Path(path).resolve() for path in paths]
firsts = []
lasts = []
for path in resolved_paths:
sorted_images = sorted(image_path for image_path in path.iterdir() if image_path.is_file())
firsts.append(sorted_images[0].stem)
lasts.append(sorted_images[-1].stem)
poster = choice(sorted_images)
fps = 24
deflicker = 3
patterns = '\n '.join(
f" '{path}/*.tif', # {first} - {last}"
for path, first, last in zip(resolved_paths, firsts, lasts, strict=True)
)
script = dedent(
f"""
from pathlib import Path
from time_lapse import make_movie, thumbnail
NAME = Path(__file__).stem
PATTERNS = [
{patterns}
]
POSTER = '{poster}'
if __name__ == '__main__':
make_movie(NAME, PATTERNS, {fps}, {deflicker}, watermark=True, verbose=False, dryrun=False)
thumbnail.create_thumbnail(NAME, Path(POSTER))
""",
).lstrip()
prefix = commonprefix([path.name for path in resolved_paths])
name = '_'.join(path.name.removeprefix(prefix) for path in resolved_paths)
if prefix:
name = f'{prefix}{name}'
save_script(name, script)
def main() -> None:
parser = argparse.ArgumentParser(description='Generate a script for creating a time-lapse movie.')
parser.add_argument(
'paths',
help='Paths to directories containing the frames for the movie.',
default='.',
nargs='+',
)
args = parser.parse_args()
if len(args.paths) == 1:
single(args.paths[0])
else:
multiple(*args.paths)
if __name__ == '__main__':
main()