Created
March 2, 2011 04:59
-
-
Save storborg/850511 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Baidu is the shit. They made a super-awesome SimCity style map. To see it, go | |
to http://maps.baidu.com and zoom in on Beijing, then click the alternate modal | |
button in the top center of the maps area. | |
Once you're down marveling in the awesomeness you will surely be inspired to | |
make a giant wall-sized mural with the tiles. | |
Use this. | |
""" | |
import os | |
import os.path | |
import urllib | |
import Image | |
import ImageChops | |
tile_dim = 256 | |
def filename(x, y, zoom): | |
work_dir = os.path.join(os.getcwd(), 'baidu-work') | |
return os.path.join(work_dir, '%d-%d,%d.jpg' % (zoom, x, y)) | |
def check(fname): | |
return os.path.exists(fname) and (os.stat(fname).st_size > 0) | |
def download(x, y, zoom): | |
""" | |
Downloader tries to be a good internet citizen by only downloading tiles | |
serially (one connection at a time) and caching tiles locally. | |
""" | |
fname = filename(x, y, zoom) | |
if not check(fname): | |
url = 'http://d0.map.baidu.com/resource/mappic/bj/2/3/lv%d/%d,%d.jpg' | |
webf = urllib.urlopen(url % (zoom, x, y)) | |
localf = open(fname, 'w') | |
localf.write(webf.read()) | |
webf.close() | |
localf.close() | |
def join(start, end, zoom): | |
print "Joining from %r to %r @ %d" % (start, end, zoom) | |
width = end[0] - start[0] | |
height = end[1] - start[1] | |
im = Image.new('RGB', (tile_dim * width, tile_dim * height)) | |
blank_tile_im = Image.open(os.path.join(os.getcwd(), 'blank-tile.jpg')) | |
watermark_antimask = make_watermark_antimask(blank_tile_im) | |
for xoff, x in enumerate(xrange(start[0], end[0])): | |
for yoff, y in enumerate(xrange(start[1], end[1])): | |
tile_im = Image.open(filename(x, y, zoom)) | |
tile_im = ImageChops.subtract(tile_im, watermark_antimask) | |
im.paste(tile_im, (xoff * tile_dim, yoff * tile_dim)) | |
return im | |
def make_watermark_antimask(im): | |
""" | |
Starting with an "empty" tile with watermark, generate a mask that can be | |
applied to other tiles to remove the watermark. | |
""" | |
original_tile = Image.new('RGB', (tile_dim, tile_dim), | |
im.getpixel((0, 0))) | |
return ImageChops.subtract(im, original_tile) | |
def main(center_x, center_y, zoom, width, height): | |
center = center_x, center_y | |
start = (center[0] - (width / 2), center[1] - (height / 2)) | |
end = (center[0] + (width / 2), center[1] + (height / 2)) | |
num_tiles = width * height | |
so_far = 0 | |
for x in xrange(start[0], end[0]): | |
for y in xrange(start[1], end[1]): | |
so_far += 1 | |
print ("Downloading (%d, %d) at %d - %d/%d" % | |
(x, y, zoom, so_far, num_tiles)) | |
download(x, y, zoom) | |
im = join(start, end, zoom) | |
joined_fname = ('joined-z%d-%dx%d-%d,%d.jpg' % | |
(zoom, width, height, center[0], center[1])) | |
im.save(os.path.join(os.getcwd(), joined_fname)) | |
if __name__ == '__main__': | |
# Centered on the forbidden city, tightest zoom level. | |
# If you want to go somewhere else, browse around the web interface and | |
# watch the URLs of the tiles being downloaded. | |
center = (2508, 2278) | |
zoom = 1 | |
width = 60 | |
height = 40 | |
main(center[0], center[1], zoom, width, height) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment