pix2.0/write_captionsJSON.py

31 lines
1006 B
Python
Raw Permalink Normal View History

2021-08-21 05:47:48 +00:00
#!/usr/bin/python3
import os
import json
import subprocess
from collections import OrderedDict
#usage: python3 write_captionsJSON.py
# do not overwrite existing captions
if not os.path.exists("js/captions.json"):
captions = {}
else:
captions = json.load(open("js/captions.json"))
subprocess.call(['cp', 'js/captions.json', 'js/captions.json.back'])
# CleanUp needed for renamed files (?how)
# cleanup deleted images
captions = { k : v for k,v in captions.items() if os.path.exists(k)}
# For each album
for path in [f for f in os.listdir('./') if os.path.isdir(f) and f != '!' and not 'videos' in f and f != 'js']:
# For each image
for name in [f for f in sorted(os.listdir(path)) if f.lower().endswith('.jpg')]:
path_name = path + "/" + name
if path_name not in captions.keys():
captions[path_name] = ""
# write 'sorted dict' to file
with open("js/captions.json","w") as j:
json.dump(OrderedDict(sorted(captions.items())), j, indent=2)