-
Notifications
You must be signed in to change notification settings - Fork 1
/
DownloadSinglePano.py
155 lines (118 loc) · 5.21 KB
/
DownloadSinglePano.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
# !/usr/bin/python
# ****************************************
# Specify the file storage location below
# ****************************************
storage_location = "/home/anthony/Downloads/test/"
from SidewalkDB import *
import os
import httplib
import json
import logging
import cStringIO
import urllib
import urllib2
from PIL import Image
from random import shuffle
import fnmatch
def download_panorama_images(storage_path, pano_list=None):
logging.basicConfig(filename='scrape.log', level=logging.DEBUG)
if pano_list is None:
unique_ids = fetch_pano_ids_from_webserver()
else:
unique_ids = pano_list
counter = 0
failed = 0
im_dimension = (512 * 26, 512 * 13)
blank_image = Image.new('RGBA', im_dimension, (0, 0, 0, 0))
base_url = 'http://maps.google.com/cbk?'
shuffle(unique_ids)
for pano_id in unique_ids:
print '-- Extracting images for', pano_id,
filename = pano_id + ".jpg"
out_image_name = filename
if os.path.isfile(out_image_name):
print 'File already exists.'
counter = counter + 1
print 'Completed ' + str(counter) + ' of ' + str(len(unique_ids))
continue
for y in range(13):
for x in range(26):
url_param = 'output=tile&zoom=5&x=' + str(x) + '&y=' + str(
y) + '&cb_client=maps_sv&fover=2&onerr=3&renderer=spherical&v=4&panoid=' + pano_id
url = base_url + url_param
# Open an image, resize it to 512x512, and paste it into a canvas
req = urllib.urlopen(url)
file = cStringIO.StringIO(req.read())
im = Image.open(file)
im = im.resize((512, 512))
blank_image.paste(im, (512 * x, 512 * y))
# Wait a little bit so you don't get blocked by Google
# sleep_in_milliseconds = float(delay) / 1000
# sleep(sleep_in_milliseconds)
print '.',
print
# In some cases (e.g., old GSV images), we don't have zoom level 5,
# so Google returns a tranparent image. This means we need to set the
# zoom level to 3.
# Check if the image is transparent
# http://stackoverflow.com/questions/14041562/python-pil-detect-if-an-image-is-completely-black-or-white
extrema = blank_image.convert("L").getextrema()
if extrema == (0, 0):
print("Panorama %s is an old image and does not have the tiles for zoom level")
temp_im_dimension = (int(512 * 6.5), int(512 * 3.25))
temp_blank_image = Image.new('RGBA', temp_im_dimension, (0, 0, 0, 0))
for y in range(3):
for x in range(7):
url_param = 'output=tile&zoom=3&x=' + str(x) + '&y=' + str(
y) + '&cb_client=maps_sv&fover=2&onerr=3&renderer=spherical&v=4&panoid=' + pano_id
url = base_url + url_param
# Open an image, resize it to 512x512, and paste it into a canvas
req = urllib.urlopen(url)
file = cStringIO.StringIO(req.read())
im = Image.open(file)
im = im.resize((512, 512))
temp_blank_image.paste(im, (512 * x, 512 * y))
# Wait a little bit so you don't get blocked by Google
# sleep_in_milliseconds = float(delay) / 1000
# sleep(sleep_in_milliseconds)
print '.',
print
temp_blank_image = temp_blank_image.resize(im_dimension, Image.ANTIALIAS) # resize
temp_blank_image.save(out_image_name, 'jpeg')
else:
blank_image.save(out_image_name, 'jpeg')
print 'Done.'
counter += 1
print 'Completed ' + str(counter) + ' of ' + str(len(unique_ids))
return
def download_panorama_depthdata(storage_path, decode=True, pano_list=None):
'''
This method downloads a xml file that contains depth information from GSV. It first
checks if we have a folder for each pano_id, and checks if we already have the corresponding
depth file or not.
'''
base_url = "http://maps.google.com/cbk?output=xml&cb_client=maps_sv&hl=en&dm=1&pm=1&ph=1&renderer=cubic,spherical&v=4&panoid="
if pano_list is None:
pano_ids = fetch_pano_ids_from_webserver()
else:
pano_ids = pano_list
for pano_id in pano_ids:
print '-- Extracting depth data for', pano_id, '...',
# Check if the directory exists. Then check if the file already exists and skip if it does.
destination_folder = os.path.join(storage_path, pano_id[:2])
if not os.path.isdir(destination_folder):
os.makedirs(destination_folder)
filename = pano_id + ".xml"
destination_file = os.path.join(destination_folder, filename)
if os.path.isfile(destination_file):
print 'File already exists.'
continue
url = base_url + pano_id
with open(destination_file, 'wb') as f:
req = urllib2.urlopen(url)
for line in req:
f.write(line)
print 'Done.'
return
pano = [sys.argv[1]]
download_panorama_images("", pano)