def get_static_google_map(request, filename=None, crop=False):
response = urlfetch.fetch(request)
# check for an error (no image at requested location)
if response.getheader('x-staticmap-api-warning') is not None:
return None
try:
img = Image.open(cStringIO.StringIO(response.content))
except IOError:
print "IOError:" # print error (or it may return a image showing the error"
return None
else:
img = np.asarray(img.convert("RGB"))
# there seems not to be any simple way to check for the gray error image
# that Google throws when going above the API limit -- so here's a hack.
if (img==224).sum() / float(img.size) > 0.95:
return None
# remove the Google watermark at the bottom of the image
if crop:
img_shape = img.shape
img = img[:int(img_shape[0]*0.85),:int(img_shape[1]*0.85)]
if filename is not None:
basedir = os.path.dirname(filename)
if not os.path.exists(basedir) and basedir not in ["","./"]:
os.makedirs(basedir)
io.imsave(filename, img)
return img
评论列表
文章目录