Skip to content

Instantly share code, notes, and snippets.

@salman-ghauri
Last active October 4, 2018 12:16
Show Gist options
  • Save salman-ghauri/87a1544d2097615d00870b7164bfd7de to your computer and use it in GitHub Desktop.
Save salman-ghauri/87a1544d2097615d00870b7164bfd7de to your computer and use it in GitHub Desktop.
Download Bing Map Bird's Eye Images. (High resolution)
"""
Use this code to download High resolution Birds Eye view which is similar to Google map's 45° imagery. There is no straight
forward way to download either of these.
"""
import urllib
import json, sys
from PIL import Image
from io import BytesIO
latlon = sys.argv[1]
key = "BING MAPS API KEY"
# latlon = "40.8495,-74.4557"
# You can set orientation by setting orientation=[0, 90, 180, 270] in the url. By default its 0 (North).
url = "https://dev.virtualearth.net/REST/v1/Imagery/Metadata/Birdseye?centerPoint={}&key={}".format(latlon, key)
content = urllib.urlopen(url).read()
resp = json.loads(content)
main = resp["resourceSets"]
for inner in main:
if inner["estimatedTotal"] > 0:
for res in inner["resources"]:
tile_url = res["imageUrl"]
subdomain = res["imageUrlSubdomains"]
zoom = res["zoomMax"]
xtile = res["tilesX"]
ytile = res["tilesY"]
tiles = xtile * ytile # total tiles: 0 - tileX * tileY
tile_width = res["imageWidth"]
tile_height = res["imageHeight"]
# Read all the image tiles and store in array.
imgs = []
for tile_id in range(tiles):
ctile = tile_url.format(subdomain=subdomain[1], zoom=zoom, tileId=tile_id)
img_content = urllib.urlopen(ctile).read()
sys.stdout.write("\r>> Got link {} out of {}".format(tile_id, tiles))
sys.stdout.flush()
imgs.append(img_content)
# Open the images in the array using ByteIO and PIL
bt = map(BytesIO, imgs)
open_images = map(Image.open, bt)
total_width = xtile * tile_width
max_height = ytile * tile_height
new_im = Image.new('RGB', (total_width, max_height))
x_offset = 0
y_offset = 0
for im in open_images:
new_im.paste(im, (x_offset, y_offset))
x_offset += im.size[0]
if x_offset == total_width:
x_offset = 0
y_offset += im.size[1]
# Save image as png name with latlong.
new_im.save("{}.png".format(latlon))
else:
sys.__stderr__.write("Nothing found at given location!\n")
@WvanWaas
Copy link

WvanWaas commented Jun 5, 2018

Nice piece of code gives great images.

@hizemj
Copy link

hizemj commented Oct 4, 2018

Is this still valid? Can't get it working
Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment