-
Notifications
You must be signed in to change notification settings - Fork 4
/
api.py
95 lines (71 loc) · 2.52 KB
/
api.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
from flask import (Flask, request, send_from_directory, redirect, url_for,
render_template, abort)
import interestingizer
from PIL import Image
from StringIO import StringIO
import requests
import json
import md5
import random
import os
app = Flask(__name__)
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 # 16Mb upload file limit
TMP_DIR = "./tmp"
ANIMALS = ["cow", "cat"]
ITEMS_BASES = dict([(a, os.path.join("./images/", a)) for a in ANIMALS])
items = dict([(a, [Image.open(os.path.join(bp, filename)).convert("RGBA")
for filename in os.listdir(bp)])
for a, bp in ITEMS_BASES.iteritems()])
@app.route("/ping")
def ping():
return "OK"
def cache_image(pil_img):
key = md5.md5(pil_img.tostring()).hexdigest()
with open(os.path.join(TMP_DIR, key), "w+") as fd:
pil_img.save(fd, 'JPEG', quality=70)
return key
@app.route("/cache/<key>")
def cache(key):
return send_from_directory(TMP_DIR, key, mimetype='image/jpeg')
@app.route("/test")
def index():
return render_template("index.html")
@app.route("/interestingize", methods=["POST", "GET"])
@app.route("/interestingize/<animal>", methods=["POST", "GET"])
def interestingize(animal="cow"):
if animal not in ANIMALS:
abort(404)
if request.method == "POST":
image_raw = request.files.get("image")
if image_raw is None:
return "Must provide image in form field 'image'", 500
try:
image = Image.open(image_raw)
except IOError:
return "Could not decode image", 500
else:
url = request.values.get("src", None)
if url is None:
return redirect(url_for("index"))
r = requests.get(url)
if r.status_code != requests.codes.ok:
r.raise_for_status()
try:
image = Image.open(StringIO(r.content))
except IOError:
return "Could not decode image", 500
item = random.choice(items[animal]).copy()
try:
better_image = interestingizer.interestingize(image, item)
except Exception, e:
print "Could not run interestingize algorithm: %s" % e
return "Could not interestingize", 500
key = cache_image(better_image)
new_url = url_for('cache', key=key)
if request.method == "POST":
return json.dumps(new_url)
return redirect(new_url)
if __name__ == "__main__":
port = int(os.environ.get('PORT', 5000))
app.debug = True
app.run(host='0.0.0.0', port=port)