-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslice.py
53 lines (39 loc) · 1.21 KB
/
slice.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
import sys
import os.path
import os
import shutil
import Image
ZOOM_MIN = 0
ZOOM_MAX = 5
TILE_WIDTH = 640
TILE_HEIGHT = 480
def make_out_dir(path):
if os.path.exists(path):
shutil.rmtree(path)
os.makedirs(path)
def open_image(path):
return Image.open(path)
def slice_img(in_path, out_path):
in_img = open_image(in_path)
x1, y1, x2, y2 = in_img.getbbox()
w = width = x2 - x1
h = height = y2 - y1
for zoom in range(ZOOM_MIN, ZOOM_MAX + 1):
path = os.path.join(out_path, str(zoom))
os.mkdir(path)
parts = 2 ** (zoom + 1)
for y_i in range(0, parts):
y_path = os.path.join(path, str(y_i))
os.mkdir(y_path)
for x_i in range(0, parts):
img_path = os.path.join(y_path, str(x_i) + '.png')
img = in_img.crop((w*x_i, h*y_i, w*(x_i + 1), h*(y_i + 1)))
img.resize((TILE_WIDTH, TILE_HEIGHT))
img.save(img_path)
w = w / 2
h = h / 2
if __name__ == '__main__':
in_path = sys.argv[1]
out_path = os.path.join(os.path.dirname(__file__), 'static', 'cart_imgs')
make_out_dir(out_path)
slice_img(in_path, out_path)