Created
November 25, 2015 06:18
-
-
Save tgs/fd79f31bfefdfd4f3bf8 to your computer and use it in GitHub Desktop.
see hidden areas in xkcd 1608 - requires bottle.py, PIL(low), numpy
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
#!/usr/bin/env python | |
import StringIO | |
import numpy as np | |
from bottle import route | |
from bottle import run | |
from bottle import abort | |
import Image | |
import requests | |
base_url = 'http://xkcd.com/' | |
@route('/<image:path>') | |
def proxy(image): | |
resp = requests.get(base_url + image) | |
if resp.status_code != 200: | |
abort(resp.status_code) | |
if 'png' in resp.headers['content-type']: | |
return munge_png(resp.content) | |
return resp.content | |
def munge_png(png_in): | |
im = Image.open(StringIO.StringIO(png_in)) | |
im = im.convert('RGBA') | |
data = np.array(im) # "data" is a height x width x 4 numpy array | |
red, green, blue, alpha = data.T # Temporarily unpack the bands for readability | |
# Replace white with red... (leaves alpha values alone...) | |
deception = (red == 1) & (blue == 1) & (green == 1) | |
data[..., :-1][deception.T] = (255, 0, 0) # Transpose back needed | |
im2 = Image.fromarray(data) | |
png_out_io = StringIO.StringIO() | |
im2.save(png_out_io, format='PNG') | |
return png_out_io.getvalue() | |
run(host='localhost', port=8080, debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment