Skip to content

Instantly share code, notes, and snippets.

@storborg
Created November 25, 2015 05:32
Show Gist options
  • Save storborg/ceeac5bf40b2ff14e2f0 to your computer and use it in GitHub Desktop.
Save storborg/ceeac5bf40b2ff14e2f0 to your computer and use it in GitHub Desktop.
import os.path
import requests
from PIL import Image
lowest_x = 928
highest_x = 1002
lowest_y = -1088
highest_y = -1069
outfile = 'out.png'
cache_dir = 'cache'
edge = 513
missing_tile_color = 255
def fetch(x, y):
basename = '%d:%d+s.png' % (x, y)
path = os.path.join(cache_dir, basename)
if not os.path.exists(path):
url = 'http://xkcd.com/1608/' + basename
print("downloading %s" % url)
resp = requests.get(url)
if resp.status_code == 404:
with open(path, 'w') as f:
f.write("404")
else:
with open(path, 'wb') as f:
f.write(resp.content)
else:
print("cached %s" % path)
if os.stat(path).st_size != 3:
return Image.open(path)
else:
return Image.new('L', (edge, edge), missing_tile_color)
def make(start, end):
startx, starty = start
endx, endy = end
dimensions = ((endx - startx) * edge, (endy - starty) * edge)
base = Image.new('L', dimensions, 0)
for x in range(startx, endx + 1):
for y in range(starty, endy + 1):
im = fetch(x, y)
offsetx = (x - startx) * edge
offsety = (y - starty) * edge
base.paste(im, (offsetx, offsety))
print("saving %d x %d PNG to %s" % (dimensions[0], dimensions[1], outfile))
base.save(outfile)
if __name__ == '__main__':
make((lowest_x, lowest_y), (highest_x, highest_y))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment