-
Notifications
You must be signed in to change notification settings - Fork 1
/
labels.py
201 lines (180 loc) · 6.33 KB
/
labels.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
# -*- coding: UTF-8 -*-
import Image
from tiles import HEIGHT, WIDTH
from locations import locations as get_locations, SCALE
from regions import regions2 as get_regions
from glob import glob
from re import compile as re
from os import stat
from os.path import isfile
from utils import makedirs
from scale import scale_command
from darken import darken
import sys
normalized_labels_re = re(r'archive/labels/(.*)\-(..)-normalized\.png')
abnormal_labels_re = re(r'archive/labels/(.*)\-([seku][lt]).png')
languages = {
"s": "Sindarin",
"e": "English",
"k": "Khuzdul",
"u": "Unknown",
}
alphabets = {
"l": "Latin",
"t": "Tengwar",
}
def normalized_labels():
for file_name in glob("archive/labels/*-normalized.png"):
parts = normalized_labels_re.match(file_name).groups()
prefix, suffix = parts
_language, _letters = suffix
language = languages[_language]
letters = alphabets[_letters]
mode = "General Use" if letters == "Tengwar" else None
mtime = stat(file_name)[9]
yield prefix, {
"prefix": prefix,
"language": language,
"letters": letters,
"mode": mode,
"mtime": mtime,
"normalized": file_name,
"thumbnail": "build/labels/%s-%s-thumbnail.png" % parts,
"resized": "build/labels/%s-%s-resized.png" % parts,
}
def normalized_labels_dict():
return dict(normalized_labels())
def abnormal_labels():
for file_name in glob("archive/labels/*-??.png"):
match = abnormal_labels_re.match(file_name)
if not match:
continue
parts = match.groups()
normalized = "archive/labels/%s-%s-normalized.png" % parts
if isfile(normalized):
continue
prefix, suffix = parts
_language, _letters = suffix
letters = alphabets[_letters]
mode = "General Use" if letters == "Tengwar" else None
mtime = stat(file_name)[9]
language = languages[_language]
yield prefix, {
"prefix": prefix,
"language": language,
"letters": letters,
"mode": mode,
"mtime": mtime,
"abnormal": file_name,
"normalized": normalized,
"thumbnail": "build/labels/%s-%s-thumbnail.png" % parts,
"resized": "build/labels/%s-%s-resized.png" % parts,
}
def abnormal_labels_dict():
return dict(abnormal_labels())
def abnormal_labels_list():
return sorted(abnormal_labels())
def differences():
locations = dict((key, value) for key, value in get_locations() if key)
labels = normalized_labels_dict()
location_prefixes = set(locations.keys())
label_prefixes = set(labels.keys())
print 'missing location data:'
for label in sorted(label_prefixes.difference(location_prefixes)):
print ' %s' % label
print 'missing normalized labels:'
for label in sorted(location_prefixes.difference(label_prefixes)):
print ' %s' % label
class Label(object):
@property
def original(self):
return 'build/labels/original/%s-%s.png' % self.parts
@property
def embedded(self):
return 'build/labels/embedded/%s-%s.png' % self.parts
@property
def practice(self):
return 'build/labels/practice/%s-%s.png' % self.parts
@property
def thumbnail(self):
return 'build/labels/thumbnails/%s-%s.png' % self.parts
mode = 'General Use'
def labels():
pngs = set(glob("archive/labels/*.png"))
normalized_pngs = set(glob("archive/labels/*-normalized.png"))
abnormal_pngs = pngs.difference(normalized_pngs)
for abnormal in sorted(abnormal_pngs):
match = abnormal_labels_re.match(abnormal)
if not match:
continue
parts = match.groups()
canonical, (_language, _letters) = parts
language = languages[_language]
letters = alphabets[_letters]
normalized = "archive/labels/%s-%s-normalized.png" % parts
yield type("%s-%s%s" % (canonical, _language, _letters), (Label,), {
"canonical": canonical,
"parts": parts,
"language": language,
"letters": letters,
"source": normalized if isfile(normalized) else abnormal,
"normalized": isfile(normalized),
})()
def labels_list():
return list(labels())
def labels_dict():
return dict(
((label.canonical, label.language, label.letters, label.mode), label)
for label in labels()
)
def build(*filter):
makedirs("build/labels/embedded")
makedirs("build/labels/practice")
makedirs("build/labels/thumbnails")
regions = get_regions()
for label in labels():
if filter and label.canonical not in filter:
continue
try:
source = Image.open(label.source)
region = regions[label.canonical]
width, height = source.size
# embedded images
outer_size = outer_width, outer_height = [
int(float(region.diagonal) * WIDTH),
int(float(region.width) / region.diagonal * region.height * HEIGHT),
]
if width > outer_width:
inner_size = [
int(outer_width),
int(float(outer_width) * height / width),
]
embedded = source.resize(inner_size, Image.ANTIALIAS)
else:
embedded = source
embedded.save(label.embedded)
practice = embedded.resize([dim / 2 for dim in embedded.size], Image.ANTIALIAS)
practice.save(label.practice)
# thumbnail
thumb_size = [
int(float(width) / height * 100),
100,
]
thumb = source.resize(thumb_size, Image.ANTIALIAS)
thumb = darken(thumb, .5)
thumb.save(label.thumbnail)
print label.parts, outer_size, source.size, embedded.size, thumb_size, practice.size
except Exception, reason:
print>>sys.stderr, label.parts, reason
def main():
import sys
from pprint import pprint
if len(sys.argv) < 2:
command = 'build'
else:
command = sys.argv[1].replace('-', '_')
result = globals()[command](*sys.argv[2:])
if result is not None:
pprint(result)
if __name__ == "__main__":
main()