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
227
228
229
230
231
|
#!/usr/bin/env python3
import jinja2
import PIL.Image
import PIL.ImageOps
import argparse
import json
import yaml
import os
import re
import hashlib
import glob
from datetime import datetime
imagereg = re.compile("([a-z0-9]*)\\..*")
thumbsize = 480
def hashfile(fname):
md5 = hashlib.md5()
print(fname)
with open(fname, "rb") as f:
for chunk in iter(lambda: f.read(4096), b""):
md5.update(chunk)
return md5
def parse_res(res):
return int(res)
class FileLoader(jinja2.BaseLoader):
def get_source(self, environment, template):
if not os.path.exists(template):
raise jinja2.TemplateNotFound(template)
mtime = os.path.getmtime(template)
with open(template, "r") as f:
source = f.read()
return source, template, lambda: mtime == os.path.getmtime(template)
class ImageLocation:
def __init__(self, folder, extension):
self.folder = folder
self.stuff = {}
self.ext = extension
self.unused = set()
self.existing = {}
if not os.path.isdir(folder):
os.mkdir(folder)
self.look()
self.tmpname = os.path.join(folder, "tmp")
def look(self):
for name in os.listdir(self.folder):
m = imagereg.match(name)
if m is not None:
fhash = m.group(1)
self.unused.add(fhash)
self.existing[fhash] = name
def convert(self, imgname, resolution):
# First determine the hash from settings
thash = hashlib.md5(
bytes(
f"{resolution}+{self.ext}" + imgname,
encoding="utf8")
).hexdigest()
# Now check if it exists
if thash in self.existing:
print(f"Skipping file {imgname}[{thash}]")
return self.existing[thash], thash
# Okay convert it
fname = f"{thash}.{self.ext}"
print(f"Converting file {imgname} to {fname}")
tmpname = f"{self.tmpname}.{self.ext}"
with PIL.Image.open(imgname) as im:
target_height = resolution
im = PIL.ImageOps.exif_transpose(im)
if im.size[0] > target_height:
res = (int((im.size[0] / im.size[1]) * target_height), target_height)
im = im.resize(res, PIL.Image.ANTIALIAS)
im.save(tmpname)
os.rename(tmpname, os.path.join(self.folder, fname))
self.existing[thash] = fname
self.unused.add(thash)
return fname, thash
def fetch(self, imgname, resolution, relto=None):
# Check if we already have it
fname, hash = self.convert(imgname, resolution)
# Mark the hash as used
self.unused.remove(hash)
if relto is not None:
full = os.path.join(self.folder, fname)
return os.path.relpath(full, start=relto)
return fname
def clean(self):
print("Running cleanup")
for uhash in self.unused:
fname = os.path.join(self.folder, self.existing[uhash])
print(f"Removing file {fname}")
os.remove(fname)
class Image:
def __init__(self, filename):
self.name = os.path.basename(filename)
self.filename = filename
self.description = ""
self.url = ""
self.thumburl = ""
self.fullurl = ""
self.file = filename
self.taken = None
self.load_metadata()
def load_metadata(self):
try:
with PIL.Image.open(self.filename) as im:
taken_str = im.getexif()[36867]
except KeyError:
print(f"Could not load date from image {self.filename}")
return
self.taken = datetime.strptime(taken_str, "%Y:%m:%d %H:%M:%S")
def value_or_default(val, default=None):
if val is None:
return default
return val
class Loader:
def __init__(self, config_file, loadpath):
self.config = Loader.__load_config(config_file)
self.loadpath = loadpath
self.images = []
def __load_config(config_file):
config = {}
with open(config_file, "r") as f:
config = yaml.safe_load(f)
print(f"Loaded config: {config}")
return config
def load(self, imagehook=None, clear=False):
# Clear if neccesary
if clear:
self.images = []
# First find all images from glob
for image in glob.iglob(os.path.join(self.loadpath, self.config["imageglob"])):
img = Image(image)
img.description = self.config["info"].get(img.name, "")
if imagehook is not None:
img = imagehook(img)
self.images.append(img)
self.images = sorted(self.images,
key=lambda img: value_or_default(img.taken,
datetime.min),
reverse=True)
def to_json(self):
jsonable = [img.__dict__ for img in self.images]
return json.dumps(jsonable)
parser = argparse.ArgumentParser()
parser.add_argument("--dest", "-d", default="dist", help="where to put resulting files")
parser.add_argument("--size", "-s", type=int, default="1080", help="size to scale web images to")
parser.add_argument("--commit", "-g", help="git commit hash to announce")
parser.add_argument("--clean", help="clean unused image files", action="store_true")
parser.add_argument("--config", "-c", default=None, help="where to load image definitions from")
parser.add_argument("--template", "-t", default="index.html.j2", help="html template to use")
parser.add_argument("--cgit", "-w", help="cgit repo base url")
parser.add_argument("load", help="where to load full size images from")
parser.add_argument("--ext", "-e", default="png", help="image extension to use")
args = parser.parse_args()
if args.config is None:
args.config = os.path.join(args.load, "imginfo.yml")
context = {
"cgit": args.cgit,
"git": args.commit
}
loader = Loader(args.config, args.load)
loc = ImageLocation(os.path.join(args.dest, "imgs"), args.ext)
def imgload(img):
if context["cgit"] is not None:
img.fullurl = os.path.join(context["cgit"], "plain", img.name)
img.url = loc.fetch(img.file, args.size, relto=args.dest)
img.thumburl = loc.fetch(img.file, thumbsize, relto=args.dest)
return img
loader.load(imgload, clear=True)
with open(os.path.join(args.dest, "imgs", "index.json"), "w") as f:
f.write(loader.to_json())
|