-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.py
65 lines (58 loc) · 1.98 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from flask import Flask, render_template, Response, request
from PIL import Image
from StringIO import StringIO
import math
import requests
import sys
import os
app = Flask(__name__)
@app.route('/')
def home():
return render_template('index.html')
@app.route('/blank')
def blank():
return render_template('blank.html')
@app.route('/gallery')
def gallery():
return render_template('gallery.html')
@app.route('/sphere')
def sphere():
return render_template('sphere.html')
@app.route('/proxy')
def proxy():
url = request.args.get('url')
# dropbox URLs have ?dl=0 which brings you to a landing page
# flip that to ?dl=1 to download the image itself
if url.find('dropbox.com') != -1:
url = url.replace('dl=0', 'dl=1')
resize = request.args.get('resize')
req = requests.get(url)
content = req.content
if resize:
image = Image.open(StringIO(req.content))
width, height = image.size
next_power_of_two = math.floor(math.log(width, 2))
if next_power_of_two > 12:
next_power_of_two = 12
new_width = 2 ** next_power_of_two
ratio = 1.0 * new_width / width
new_height = height * ratio
image = image.resize((int(new_width), int(new_height)))
imagefile = StringIO()
image.save(imagefile, format='JPEG')
content = imagefile.getvalue()
return Response(content, content_type=req.headers['content-type'])
if __name__ == '__main__':
debug = False
if len(sys.argv) > 1 and sys.argv[1] == 'debug':
debug = True
extra_dirs = ['./',]
extra_files = extra_dirs[:]
for extra_dir in extra_dirs:
for dirname, dirs, files in os.walk(extra_dir):
for filename in files:
filename = os.path.join(dirname, filename)
if os.path.isfile(filename):
extra_files.append(filename)
threaded = not debug
app.run(host='0.0.0.0', debug=debug, port=9090, extra_files=extra_files, threaded=threaded)