-
Notifications
You must be signed in to change notification settings - Fork 0
/
tile-merge.py
94 lines (70 loc) · 2.51 KB
/
tile-merge.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
## README
# Tile Download and Stitcher
# Python 3 Download Script
# https://github.com/oculus42/py-tile-merge
#
# Image Merge uses the Pillow Library
# On windows, install Pillow with the following command
#
# py -m pip install Pillow
################################################
# Change these variables
start_x = 0
end_x = 10
start_y = 0
end_y = 10
z_level = 13
# Optional delay between successive requests (in seconds)
request_delay = 0.5
# Swaps out the x, y, and z values with %s, which allows the script to fill them in
# This script doesn't support reordering those parameters at this time.
base_url = "http://www.example.com/tileService?x=%s&y=%s&z=%s"
source_type = "jpg"
################################################
# Everything below this should stay the same
import os
import os.path
import urllib
import urllib.request
import time
# Make an output folder
if not os.path.exists("out"):
os.makedirs("out")
# Python range is exclusive of the end, so we add one
for x in range(start_x, end_x + 1):
for y in range(start_y, end_y + 1):
dl_url = base_url % (x, y, z_level)
file_name = "out/%s-%s-%s.%s" % (x, y, z_level, source_type)
#Don't re-download images we have
if not os.path.isfile(file_name):
print(file_name)
urllib.request.urlretrieve(dl_url,file_name)
# Don't overload the server with requests
time.sleep(request_delay)
# Image Merge section
import sys
from PIL import Image
start_image_path = "out/%s-%s-%s.%s" % (start_x, start_y, z_level, source_type)
# Figure out the final image size
# Assumes all tiles are the same size
start_tile = Image.open(start_image_path)
tile_width = start_tile.size[0]
tile_height = start_tile.size[1]
start_tile.close()
total_width = tile_width * (end_x - start_x + 1)
total_height = tile_height * (end_y - start_y + 1)
print("Final image is %s x %s" % (total_width, total_height))
final_image = Image.new('RGB', (total_width, total_height))
# Same range logic as above
# This time we cycle over the saved images
# Pasting them into the final as we go
for x in range(start_x, end_x + 1):
for y in range(start_y, end_y + 1):
file_name = "out/%s-%s-%s.%s" % (x, y, z_level, source_type)
tile = Image.open(file_name)
x_offset = (x) * tile_width
y_offset = (y) * tile_height
final_image.paste(tile, (x_offset, y_offset))
tile.close()
# Full quality on the output image
final_image.save("complete.jpg", quality=100)