-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgallery.py
226 lines (163 loc) · 6.62 KB
/
gallery.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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
"""webnote.Gallery classes to handle directories of picture files.
"""
import datetime
import os
import pytz
import subprocess
from directory import Directory
from picture import Picture
from PIL import Image
import settings
class Gallery():
dirpath = None
docroot = None
baseurl = None
address = None
gallery = None
def __init__(self, docroot, baseurl, address):
"""Build the data structure describing this gallery."""
dirpath = os.path.join(docroot, address)
if not os.path.isdir(dirpath):
raise self.DirectoryNotFound(dirpath)
self.address = address
self.baseurl = baseurl
self.dirpath = dirpath
self.docroot = docroot
self.paired = Directory(dirpath, docroot=docroot, baseurl=baseurl)
class DirectoryNotFound(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
def __get_absolute_url__(self):
return os.path.join(self.baseurl, self.address)
url = property(__get_absolute_url__)
def gpxfiles(self):
"""Return the list of GPX file filenames."""
return self.paired.model['gpx']
def pictures(self, docroot=None, baseurl=None):
"""Return a list of picture objects."""
pictures = []
if not docroot:
docroot = self.docroot
if not baseurl:
baseurl = self.baseurl
for pic in self.paired.model['pictures']:
fname = os.path.join(self.dirpath, pic)
picture = Picture(fname, docroot=docroot, baseurl=baseurl)
pictures.append(picture)
return pictures
def accession_pictures(self):
"""Process pictures into thumbnails.
For each picture file
"""
warnings = []
if not os.path.isdir(self.d1024()):
warnings.append("Creating directory at " + self.d1024())
os.mkdir(self.d1024())
if not os.path.isdir(self.d512()):
warnings.append("Creating directory at " + self.d512())
os.mkdir(self.d512())
for picture in self.paired.model['pictures']:
path = os.path.join(self.dirpath, picture)
(basename, ext) = os.path.splitext(picture)
f1024 = basename + '_1024px' + ext.lower()
path1024 = os.path.join(self.dirpath, self.d1024(), f1024)
f512 = basename + '_512px' + ext.lower()
path512 = os.path.join(self.dirpath, self.d512(), f512)
if os.path.isfile(path):
i = Image.open(path)
i.thumbnail((1024, 1024))
i.save(path1024, "jpeg")
i.thumbnail((512, 512))
i.save(path512, "jpeg")
warnings.append("Creating thumbnail copies.")
return warnings
def processed(self):
"""True or false. Have the pictures here been processed?
This looks for the presence of subdirectories with names
appearing in settings.FILEMAP_PICTURES.
"""
for item in self.paired.model['dirs']:
if item in settings.FILEMAP_PICTURES['1024px']:
return True
if item in settings.FILEMAP_PICTURES['512px']:
return True
return False
def d1024(self):
"""Pathname to 1024px directory."""
return os.path.join(
self.dirpath, settings.FILEMAP_PICTURES['1024px'][0],
)
def d512(self):
return os.path.join(
self.dirpath, settings.FILEMAP_PICTURES['512px'][0],
)
def process_gps(self, pictime, gpstime, tzoffset):
"""Run gpscorrelate against gpx files found in this directory.
pictime will be naive, gpstime will be UTC.
First, convert the picture time to UTC using the timezone
offset. Then compute the difference in time between the photo
and gps time signatures. With these data, you can compose the
gpscorrelate command to be run in a shell.
The variable tzoffset is expected as a string in the manner of
'+1200'.
From the man gpscorrelate page:
gpscorrelate [-z | --timeadd +/-HH[:MM]] [-O | --photooffset seconds]
[-i | --no-interpolation] [-v | --verbose] [-d |
--datum datum] [-n | --no-write] [-m | --max-dist time]
[-t | --ignore-tracksegs] [-M | --no-mtime] [-f |
--fix-timestamps] [-p | --degmins] -g file.gpx
image.jpg...
-O, --photooffset seconds
time in seconds to add to the photo timestamp to make it match the
GPS timestamp. To determine the amount of seconds needed, just
create a picture of your GPS device showing the current time and
compare it with the timestamp of your photo file.
"""
warnings = []
if not gpstime:
warnings.append("GPS time required.")
warnings.append("No correlation performed.")
return warnings
if not tzoffset:
warnings.append("Timezone offset required.")
warnings.append("No correlation performed.")
return warnings
photooffset = pictime - gpstime
photooffset = photooffset.seconds
if gpstime < pictime:
photooffset = photooffset * -1
# Process tzoffset into HH:MM.
h = tzoffset[:3]
m = tzoffset[-2:]
Z = h + ':' + m
# Provide a list of picture file extensions.
extensions = []
for thing in self.paired.model['pictures']:
(basename, ext) = os.path.splitext(thing)
if ext not in extensions:
extensions.append(ext)
# Compile a list of gpscorrelate commands.
precom = "gpscorrelate -M -z " + Z + " "
precom += "-O " + str(photooffset) + " "
precom += "-g "
commands = []
for gpxfile in self.gpxfiles():
gpxfname = os.path.join(
self.dirpath, gpxfile.replace(' ', '\ ')
)
command = precom + gpxfname
for ext in extensions:
fullcommand = command + " " + self.dirpath + "/*" + ext
commands.append(fullcommand)
# Execute the commands and return the output.
outputs = []
os.chdir(self.dirpath)
for line in commands:
try:
output = subprocess.check_output(line, shell=True)
except subprocess.CalledProcessError:
output = "Something went wrong. No correlation was performed."
outputs.append("<pre>" + output + "</pre>")
return outputs