-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcaption.py
43 lines (31 loc) · 1.17 KB
/
caption.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
import json
import os
from collections import namedtuple
import logging
log = logging.getLogger(__name__)
def caption_decode(caption_dict):
return namedtuple('X', caption_dict.keys())(*caption_dict.values())
def read_caption(file):
with open(file, 'r') as myfile:
text = myfile.read()
return text
def check_for_caption(filepath):
'''check if folder has json info'''
log.info('checking for caption file')
base_dir = os.path.dirname(filepath)
if os.path.exists(os.path.join(base_dir, 'caption.json')):
log.info(f'caption file found, returning {os.path.join(base_dir, "caption.json")}')
return os.path.join(base_dir, 'caption.json')
return None
def generate_caption(filepath):
results = read_caption(filepath)
caption = json.loads(results, object_hook=caption_decode)
tags = ''
for tag in caption.tags:
tags = f'{tags}\n#{tag}'
return f'{caption.message}{tags}'
def get_caption(image_path):
caption_file = check_for_caption(image_path)
if caption_file:
return generate_caption(caption_file)
return f'#{os.path.basename(os.path.dirname(image_path))} #eddyizm | https://eddyizm.com'