-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsplit.py
More file actions
78 lines (61 loc) · 2.16 KB
/
split.py
File metadata and controls
78 lines (61 loc) · 2.16 KB
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
#!/usr/bin/python
import os
import sys
import math
from PIL import Image
from plotconfig import splitXInc, splitYInc
def splitter(p, fn, img=None, maxZoom=7):
try:
fileFullName = os.path.realpath(fn)
except:
raise Exception('File to be treated not given.')
pathBase = os.path.realpath(os.path.dirname(fileFullName))
fileName = os.path.basename(fileFullName)
print "> Crop image at %s" % fileFullName
if None == img:
img = Image.open(fileFullName)
print "> Read in image."
for zoomLevel in xrange(4, maxZoom + 1):
count = (1 << zoomLevel)
gridDegreeX = 360.0 / count
gridDegreeY = 360.0 / count
for x in xrange(0, count):
cropW = x * gridDegreeX - 180
cropE = cropW + gridDegreeX
outPath = os.path.join(\
pathBase,
fileName + '-split',
str(zoomLevel),
str(x)
)
outPathCreated = False
cropS = 180.0 # who knows how the developer of Leaflet.js thought
for y in xrange(0, count):
cropS -= gridDegreeY
cropN = cropS + gridDegreeY
if abs(cropN) > 90.0:
continue
try:
crop = p.cropAndResize(img, (cropN, cropW, cropS, cropE))
except:
# print (cropN, cropW, cropS, cropE)
continue
if not crop:
continue
imgFormat = '.png'
if zoomLevel <= maxZoom - 1:
crop = crop.convert("RGB")
imgFormat = '.jpg'
if not outPathCreated:
try:
os.makedirs(outPath)
outPathCreated = True
except Exception,e:
print e
print outPath
continue
if outPathCreated:
crop.save(\
os.path.join(outPath, str(y) + imgFormat),
quality=70
)