-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathadd_to_gallery.py
More file actions
executable file
·120 lines (78 loc) · 2.89 KB
/
add_to_gallery.py
File metadata and controls
executable file
·120 lines (78 loc) · 2.89 KB
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
114
115
116
117
118
119
120
import typing as ty
import argparse as argp
import sys
import os
import re
#import cv2
#import numpy as np
import datetime as dt
import yaml
sizes = \
{
'xs': 340,
'sm': 480,
'md': 640,
'lg': 820
}
def main():
arguments = parse_arguments(sys.argv[1:])
image_dir:str = arguments.image_folder
if image_dir.endswith('/'):
image_dir = image_dir[:-1]
files = filter_directory(image_dir)
#resize_images(image_dir, files)
if arguments.date == '':
date = dt.date.today().strftime('%Y-%m-%d')
else:
date = arguments.date
if arguments.name == '':
name = os.path.basename(image_dir)
else:
name = arguments.name
write_manifest(files, name, date, arguments.output)
def parse_arguments(args: ty.List[str]) -> argp.Namespace:
parser = argp.ArgumentParser()
parser.add_argument('image_folder', metavar='image-folder')
parser.add_argument('-o', '--output', default='_photos', help='file name to put data in by default date-name.md')
parser.add_argument('-d', '--date', default='', help='Date of gathering')
parser.add_argument('-n', '--name', default='', help='Name of gathering')
return parser.parse_args(args)
def is_name_allowed(name: str) -> bool:
for key in sizes.keys():
if name.startswith(f'{key}-'):
return False
return True
def filter_directory(dir: str) -> ty.List[str]:
dir_iter = iter(os.walk(dir))
root, _, files = next(dir_iter)
base_name = os.path.basename(root)
#files = list(filter(is_name_allowed, files))
files = list(map(lambda file: os.path.join(root, file), files))
return files
def resize_images(root: str, images: ty.List[str]) -> None:
for image in images:
img: np.ndarray = cv2.imread(image)
for name, new_width in sizes.items():
new_height = round(new_width / (img.shape[1] / img.shape[0]))
new_img = cv2.resize(img, dsize=(new_width, new_height), interpolation=cv2.INTER_AREA)
cv2.imwrite(os.path.join(root, f'{name}-{os.path.basename(image)}'), new_img)
def generate_image_names(image: str)->ty.Dict[str,str]:
dir_name = os.path.dirname(image)
image = os.path.basename(image)
res = {}
for size in sizes.keys():
res[size] = os.path.join(dir_name, f'{size}-{image}')
return res
def write_manifest(images: ty.List[str], name: str, date: str, output_dir: str) -> None:
file_name = os.path.basename(os.path.dirname(images[0]))
#images = list(map(generate_image_names, images))
res = {}
res['name'] = name
res['date'] = date
res['images'] = images
cont = yaml.dump(res, default_flow_style=False)
with open(os.path.join(output_dir, f'{date}-{file_name}.md'), 'w') as f:
f.writelines(['---\n', cont, '---\n'])
f.flush()
if __name__ == '__main__':
main()