Repository navigation
Expand file tree
/
Copy pathgenerate_gallery.py
More file actions
51 lines (42 loc) · 1.54 KB
/
Copy pathgenerate_gallery.py
File metadata and controls
51 lines (42 loc) · 1.54 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
"""
generate_gallery.py
Scans the local `photos/` directory for subfolders and image files and writes
`gallery_data.json` mapping album names -> ordered list of relative image paths.
Run:
python generate_gallery.py
This script is intentionally simple and safe for static sites. It includes
common image extensions and preserves relative paths suitable for linking
from `gallery.html`.
"""
from pathlib import Path
import json
IMG_EXTS = {'.jpg', '.jpeg', '.png', '.gif', '.webp', '.bmp', '.tiff'}
ROOT = Path(__file__).parent
PHOTOS_DIR = ROOT / 'photos'
OUT_FILE = ROOT / 'gallery_data.json'
def make_gallery_data(photos_dir: Path):
data = {}
if not photos_dir.exists():
print(f"Photos directory not found: {photos_dir}")
return data
for child in sorted(photos_dir.iterdir()):
if child.is_dir():
imgs = []
for p in sorted(child.rglob('*')):
if p.suffix.lower() in IMG_EXTS and p.is_file():
# produce a path relative to the site root, suitable for <img src="...">
rel = p.relative_to(ROOT).as_posix()
imgs.append(rel)
if imgs:
data[child.name] = imgs
return data
def main():
data = make_gallery_data(PHOTOS_DIR)
if not data:
print('No galleries found or no images in photos/.')
else:
with OUT_FILE.open('w', encoding='utf-8') as f:
json.dump(data, f, indent=2)
print(f'Wrote {OUT_FILE} with {len(data)} galleries.')
if __name__ == '__main__':
main()