-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutil.py
217 lines (163 loc) · 6.66 KB
/
util.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
import unittest
import sys
from PIL import Image
import log
def get_default(dic, k, fallback, cast=None):
retval = fallback
if (k in dic): retval = dic[k]
#else: log.log.write("Warning: Unspecified setting %s, defaulting to %s\n" % (k, fallback))
if (cast is not None): retval = cast(retval)
return retval
# If the image can't be loaded, return a default of the specified dimensions
# If accept_dimension is specified, scale non-matching image to that size
def default_image(path, default_dimension=(300,800), accept_dimension=None):
try:
loaded = Image.open(path)
loaded.load()
except:
log.log.write("Unable to load image %s. Falling back to plain.\n" % path)
return Image.new("RGBA", default_dimension, (230, 230, 255, 255))
if (accept_dimension != None):
if (loaded.size != accept_dimension):
log.log.write("Image sizes not matching. Resizing to %d x %d\n" % accept_dimension)
return loaded.resize(accept_dimension, Image.BICUBIC)
# Image is good enough as it is.
return loaded
def rotate_image(image, rotation):
""" Rotate a PIL image, returning a minimal bounding image """
if (rotation == 0): return image
# Give enough space in all directions to properly rotate
maxdim = max(image.height, image.width)
rotimg = Image.new("RGBA", (maxdim*2, maxdim*2), (0, 0, 0, 0))
rotimg.paste(image,
(maxdim - int(image.width/2), maxdim - int(image.height/2)), # Center the image
mask=image)
rotimg = rotimg.rotate(rotation)
# Trim off the excess
return rotimg.crop(rotimg.getbbox())
def alignment_to_absolute(origin, dimensions, alignment_x="left", alignment_y="top"):
""" Convert a specified location (origin + alignment) to absolute top-left coordinates """
x,y = origin
w,h = dimensions
# Horizontal alignment
if (alignment_x == "center"): x = int(x - (w/2))
if (alignment_x == "right"): x = int(x - w)
# Vertical alignment
if (alignment_y == "center"): y = int(y - (h/2))
if (alignment_y == "bottom"): y = int(y - h)
return (x,y)
def aligned_maxdims(origin, dims, container_dims, alignment_x="left", alignment_y="top"):
""" Determine the maximum dimensions of an object inside a container of fixed size. """
x, y = origin
w, h = dims
cw, ch = container_dims
# This needs to take into account the alignment (X)
maxwidth = min(cw - x, w)
if (alignment_x == "center"):
maxwidth = min(2*x, 2*(cw - x)) # Edges of the container
maxwidth = min(maxwidth, w) # Specified width
elif (alignment_x == "right"):
maxwidth = min(x, w)
# Likewise, we need to find the max height including the edges of the card.
maxheight = min(ch - y, h)
if (alignment_y == "center"):
maxheight = min(2*y, 2*(ch - y)) # Edges of the container
maxheight = min(maxheight, h) # Specified height
elif (alignment_y == "bottom"):
maxheight = min(y, h)
return (maxwidth, maxheight)
#
# Unit tests
#
class TestUtils(unittest.TestCase):
def test_default(self):
dic = { "text" : "abcd", "number" : 3.14, "numeric" : "34" }
self.assertEqual(get_default(dic, "text", "xyz"), "abcd")
self.assertEqual(get_default(dic, "notext", "xyz"), "xyz")
self.assertEqual(get_default(dic, "number", 8), 3.14)
self.assertEqual(get_default(dic, "number", 16, int), 3)
self.assertEqual(get_default(dic, "numeric", 12, int), 34)
self.assertEqual(get_default(dic, "not-a-number", 8), 8)
def test_alignment_to_absolute(self):
pos = alignment_to_absolute((20,30), (10, 15), "left", "top")
self.assertEqual(pos, (20,30))
pos = alignment_to_absolute((20,30), (10, 15), "center", "top")
self.assertEqual(pos, (15,30))
pos = alignment_to_absolute((20,30), (10, 15), "right", "top")
self.assertEqual(pos, (10,30))
pos = alignment_to_absolute((20,30), (10, 15), "left", "center")
self.assertEqual(pos, (20,22))
pos = alignment_to_absolute((20,30), (10, 15), "left", "bottom")
self.assertEqual(pos, (20,15))
def test_align_maxdims(self):
# Left-aligned
#
# No edges
dims = aligned_maxdims((20, 30), (100, 200), (300, 400), "left", "top")
self.assertEqual(dims, (100, 200))
# Right edge
dims = aligned_maxdims((20, 30), (100, 200), (100, 400), "left", "top")
self.assertEqual(dims, (80, 200))
# Horizontal centering
#
# No edges
dims = aligned_maxdims((150, 30), (100, 200), (300, 400), "center", "top")
self.assertEqual(dims, (100, 200))
# Right edge
dims = aligned_maxdims((280, 30), (140, 200), (300, 400), "center", "top")
self.assertEqual(dims, (40, 200))
# Left edge
dims = aligned_maxdims((30, 30), (140, 200), (300, 400), "center", "top")
self.assertEqual(dims, (60, 200))
# Right-aligned
#
# No edges
dims = aligned_maxdims((120, 30), (100, 200), (300, 400), "right", "top")
self.assertEqual(dims, (100, 200))
# Right edge
dims = aligned_maxdims((120, 30), (140, 200), (100, 400), "right", "top")
self.assertEqual(dims, (120, 200))
# Top-aligned
#
# No edges
dims = aligned_maxdims((20, 30), (10, 200), (300, 400), "left", "top")
self.assertEqual(dims, (10, 200))
# Bottom edge
dims = aligned_maxdims((20, 210), (10, 200), (300, 400), "left", "top")
self.assertEqual(dims, (10, 190))
# Vertical centering
#
# No edges
dims = aligned_maxdims((20, 110), (100, 200), (300, 400), "right", "center")
self.assertEqual(dims, (20, 200))
# Top edge
dims = aligned_maxdims((20, 100), (110, 250), (300, 400), "right", "center")
self.assertEqual(dims, (20, 200))
# Bottom edge
dims = aligned_maxdims((20, 300), (110, 250), (300, 400), "right", "center")
self.assertEqual(dims, (20, 200))
# Bottom-aligned
#
# No edges
dims = aligned_maxdims((20, 230), (10, 200), (300, 400), "left", "bottom")
self.assertEqual(dims, (10, 200))
# Top edge
dims = aligned_maxdims((20, 40), (10, 200), (300, 400), "left", "bottom")
self.assertEqual(dims, (10, 40))
def test_rotate_image(self):
# A block that's pink on one side and blue on the other
pink = (255, 50, 200, 255)
blue = (0, 50, 255, 255)
left = Image.new("RGBA", (40, 40), pink)
box = Image.new("RGBA", (80, 40), blue)
box.paste(left, (0, 0))
stand = rotate_image(box, 90)
self.assertEqual(stand.size, (40, 80))
self.assertEqual(stand.getpixel((0,0)), blue)
self.assertEqual(stand.getpixel((0,79)), pink)
fall = rotate_image(box, -90)
self.assertEqual(fall.size, (40, 80))
self.assertEqual(fall.getpixel((0,0)), pink)
self.assertEqual(fall.getpixel((0,79)), blue)
if __name__ == '__main__':
unittest.main()