-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_database.py
64 lines (45 loc) · 1.39 KB
/
image_database.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
import matplotlib.pyplot as plt
import datetime
from subprocess import Popen, PIPE
from sys import argv
import os
def normPath(path):
# clean up
if path[-1]=='/':
path=path[:-1]
path = os.path.expanduser(path)
return path
def saveFiguesWithData(path, figure_list, data, prefix=""):
from pyexif import ExifEditor
path = normPath(path)
# create path if necessariy
if not os.path.exists(path):
os.makedirs(path)
date_str = datetime.datetime.now().isoformat().replace(':','-')
if not(isinstance(data,str)):
repo_rev = Popen(['hg','identify','-n'], shell=False, stdout=PIPE).stdout.read()
data['revision']=repo_rev.strip()
data['file']=argv[0]
keywords = []
for name,value in data.iteritems():
keywords.append("{}:{}".format(name,value))
keywords = ";".join(keywords)
else:
keywords = data
fig_names = []
for name,fig in figure_list.iteritems():
figname = "{}/{}_{}-{}.jpg".format(path,prefix,name,date_str)
fig_names.append("{}_{}-{}.jpg".format(prefix,name,date_str))
fig.savefig(figname)
E = ExifEditor(figname)
if isinstance(data,str):
for pair in data.split(';'):
E.addKeyword(pair)
else:
for name,value in data.iteritems():
E.addKeyword("{}:{}".format(name,value))
fig_names = ";".join(fig_names)
with open("{}/{}".format(path,'log.log'),'a+') as f:
record = "\t".join((date_str,keywords,fig_names))
f.write(record+'\n')
pass