Skip to content

Instantly share code, notes, and snippets.

@gardiner
Created September 19, 2012 08:27
Show Gist options
  • Save gardiner/3748399 to your computer and use it in GitHub Desktop.
Save gardiner/3748399 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import os
import urllib2
from PIL import Image
BASE = 'http://imgs.xkcd.com/clickdrag'
N, E, S, W = [14,48,25,33]
def tiles():
"""Generator iterating over all available tile filenames.
"""
for v in range(-N, S):
for h in range(-W, E):
vcoord = "%in" % (-v)
hcoord = "%sw" % (-h)
if v >= 0:
vcoord = "%is" % (v + 1)
if h >= 0:
hcoord = "%ie" % (h + 1)
filename = "%s%s.png" % (vcoord, hcoord)
#print filename
yield (h, v), filename
def fetch():
for pos, filename in tiles():
url = "%s/%s" % (BASE, filename)
try:
data = urllib2.urlopen(url)
with open(filename, 'w') as f:
f.write(data.read())
except urllib2.HTTPError:
print "Skipping %s" % url
def compose(s=256):
comp = Image.new('RGB', ((E + W) * s, (N + S) * s))
black = Image.new('RGB', (s, s), '#000000')
white = Image.new('RGB', (s, s), '#ffffff')
for pos, filename in tiles():
h, v = pos
if os.path.exists(filename):
i = Image.open(filename).resize((s, s), Image.ANTIALIAS)
elif v >= 0:
i = black
else:
i = white
x = (h + W) * s
y = (v + N) * s
comp.paste(i, (x, y))
comp.save('xkcdworld_%i.png' % s)
if __name__ == '__main__':
#fetch()
compose()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment