-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-new-images.py
executable file
·113 lines (99 loc) · 3.05 KB
/
generate-new-images.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
111
112
113
#!/usr/bin/env python3
import argparse
import json
import os
import shutil
METADATA_TEMPLATE = {
"alt": "",
"camera": "",
"date": "",
"film": "",
"subtitle": "",
}
OUTPUT_DIR = "queued"
IMAGE_DIR = os.path.join(OUTPUT_DIR, "images")
METADATA_DIR = os.path.join(OUTPUT_DIR, "metadata")
def unused(dates: list[tuple[str, str]], new_image: str) -> bool:
for date, image in dates:
if image == new_image:
print(f"{image} is already used on {date}")
return False
if os.path.exists(os.path.join(IMAGE_DIR, new_image)):
print(f"{new_image} is already in {IMAGE_DIR}")
return False
return True
def write_metadata(json_name: str) -> int:
print(f"Creating {json_name}")
try:
with open(json_name, "w") as f:
json.dump(
METADATA_TEMPLATE,
f,
sort_keys=True,
indent=2,
)
f.write(os.linesep)
except (
FileNotFoundError,
json.decoder.JSONDecodeError,
) as e:
print(
f"Unable to write metadata: {json_name}.",
e,
)
return 1
return 0
def main(argv: list[str] | None = None) -> int:
parser = argparse.ArgumentParser(
description="copy and create metadata files for new images",
)
parser.add_argument(
"--config-file",
help="Path to config file. defaults to ./config.json",
default="config.json",
)
parser.add_argument(
"source_dir",
help="Source directory for images",
)
args = parser.parse_args(argv)
try:
with open(args.config_file) as c:
config = json.load(c)
except (FileNotFoundError, json.decoder.JSONDecodeError) as e:
print(f"Unable to load config_file: {args.config_file}.", e)
return 1
dates = config.get("dates", None)
if dates is None:
print("Unable to get dates from config")
return 1
if not os.path.exists(args.source_dir):
print(f"Error: unable to list {args.source_dir}")
return 1
with os.scandir(args.source_dir) as it:
for entry in it:
if entry.is_file():
name = entry.name
prefix, ext = os.path.splitext(name)
if ext == ".jpg" and unused(dates, name):
print(
f"Moving {
args.source_dir
}/{name} "
f"to {IMAGE_DIR}/{name}",
)
shutil.move(
os.path.join(args.source_dir, name),
os.path.join(IMAGE_DIR, name),
)
ret = write_metadata(
os.path.join(
METADATA_DIR,
prefix + os.path.extsep + "json",
),
)
if ret != 0:
return ret
return 0
if __name__ == "__main__":
exit(main())