-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtiles.py
172 lines (159 loc) · 4.59 KB
/
tiles.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
import Image
HEIGHT = WIDTH = 32768
TILE_HEIGHT = 256
TILE_WIDTH = 256
SCALE = 2
DEPTH = None
def contains((outer_left, outer_top, outer_width, outer_height), (inner_left, inner_top)):
"""
>>> contains((0, 0, 1, 1), (0, 0))
True
>>> contains((0, 0, 1, 1), (.5, .5))
True
>>> contains((0, 0, 1, 1), (0, .5))
True
>>> contains((0, 0, 1, 1), (.5, 0))
True
>>> contains((0, 0, 1, 1), (0, 1))
False
>>> contains((0, 0, 1, 1), (1, 1))
False
>>> contains((0, 0, 1, 1), (1, 0))
False
>>> contains((0, 0, 1, 1), (-1, -1))
False
"""
return (
inner_left >= outer_left and
inner_left < outer_left + outer_width and
inner_top >= outer_top and
inner_top < outer_top + outer_height
)
def overlap(outer, (inner_left, inner_top, inner_width, inner_height)):
"""
>>> overlap((0, 0, 2, 2), (0, 0, 2, 2))
True
>>> overlap((0, 0, 2, 2), (1, 1, 2, 2))
True
>>> overlap((0, 0, 2, 2), (2, 2, 2, 2))
False
>>> overlap((0, 0, 2, 2), (-1, -1, 2, 2))
True
"""
return any(contains(outer, corner) for corner in [
(left, top)
for left in (inner_left, inner_left + inner_width)
for top in (inner_top, inner_top + inner_height)
])
def walk(
quadkey = "",
x = 0,
y = 0,
width = WIDTH,
height = HEIGHT,
depth = 0,
region = None,
scales = None,
):
"""
>>> list(walk(region = (0, 0, 0, 0)))
['', '0', '00', '000', '0000', '00000', '000000', '0000000']
>>> list(walk(region = (0, 0, 0, 0), scales = (0,)))
['']
>>> list(walk(region = (-1, -1, 0, 0), scales = (0,)))
[]
>>> list(walk(region = (WIDTH / 2, HEIGHT / 2, 0, 0), scales = (0, 1,)))
['', '3']
>>> list(walk(region = (WIDTH / 2, HEIGHT / 2, 0, 0), scales = (1,)))
['3']
>>> list(walk(scales = (0, 1,)))
['', '0', '1', '2', '3']
>>> list(walk(scales = (1,)))
['0', '1', '2', '3']
"""
if DEPTH is not None and depth > DEPTH:
return
if width < TILE_WIDTH or height < TILE_HEIGHT:
return
if region is not None:
if not overlap((x, y, width, height), region):
return
_width, _height = width / SCALE, height / SCALE
if scales is not None:
scale = len(quadkey)
if scale in scales:
yield quadkey
else:
yield quadkey
for _y in range(2):
for _x in range(2):
for tile in walk(
quadkey + "0123"[_y * 2 + _x],
x + _x * _width,
y + _y * _height,
_width,
_height,
depth = depth + 1,
region = region,
scales = scales,
):
yield tile
def quad(crop, prefix, suffix, x, y, width, height, depth = 0):
if DEPTH is not None and depth > DEPTH:
return
if width < TILE_WIDTH or height < TILE_HEIGHT:
return
_width, _height = width / SCALE, height / SCALE
print prefix + suffix, x, y, width, height
crop.resize((TILE_WIDTH, TILE_HEIGHT), Image.ANTIALIAS).save(prefix + suffix)
for _y in range(2):
for _x in range(2):
_crop = crop.crop((
_x * _width,
_y * _height,
_x * _width + _width,
_y * _height + _height,
))
quad(
_crop,
prefix + "0123"[_y * 2 + _x],
suffix,
x + _x * _width,
y + _y * _height,
_width,
_height,
depth + 1
)
def seed(file_name, prefix, suffix):
image = Image.open(file_name)
width, height = (int(n) for n in image.size)
quad(image, prefix, suffix, 0, 0, width, height)
def coord(key, region):
"""
>>> coord("", (0, 0, 1, 1))
(0, 0, 1, 1)
>>> coord("0", (0, 0, 2, 2))
(0, 0, 1, 1)
>>> coord("1", (0, 0, 2, 2))
(1, 0, 1, 1)
>>> coord("3", (0, 0, 2, 2))
(1, 1, 1, 1)
"""
x, y, width, height = region
for s in key:
width /= 2
height /= 2
s = int(s)
x += bool(s & 1) * width
y += bool(s & 2) * height
return (x, y, width, height)
def command(source, target):
seed(source, target, ".png")
def main():
import sys
command(sys.argv[1], sys.argv[2])
def test():
from doctest import testmod
testmod()
if __name__ == '__main__':
main()