-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
150 lines (130 loc) · 6.12 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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import base64
import io
import os
import tempfile
import warnings
import lamps
from PIL import Image
from flask import Flask, flash, redirect, render_template, request, send_from_directory, url_for
from werkzeug.utils import secure_filename
application = Flask(__name__)
application.config['MAX_CONTENT_PATH'] = 32 * 1024 * 1024
@application.route('/')
def hello_world():
return render_template('index.html')
@application.route('/hips.html')
def hips():
return render_template('hips.html')
@application.route('/foot.html')
def foot():
return render_template('foot.html')
@application.route('/about_factorio_lamps.html')
def about_lamps():
return render_template('about_factorio_lamps.html')
@application.route('/favicon.ico')
def favicon():
return send_from_directory(os.path.join(application.root_path, 'static'),
'favicon.ico',
mimetype='image/vnd.microsoft.icon')
MAX_LAMPS = 50000
@application.route('/factorio_lamps', methods=['GET', 'POST'])
def process_lamps():
if request.method == 'POST':
# check if the post request has the file part
# there are two ways to get a file: from the cache
# or as an attachment to the POST request
# cached files are saved as temporary files, so they
# will eventually expire
if 'image' in request.form and request.form['image'] == 'cache':
cache_filename = secure_filename(request.form['cachefile'])
cache_dir = secure_filename(request.form['cachedir'])
tmp_file = os.path.join(tempfile.gettempdir(), cache_dir, cache_filename)
print('Attempting to reuse %s' % tmp_file)
try:
image = lamps.open_rotated_image(tmp_file)
except FileNotFoundError:
return render_template('lamp.html',
error='Sorry, the cache file no longer exists')
except OSError:
return render_template('lamp.html',
error='Sorry, the cache file has been corrupted')
else:
if 'file' not in request.files:
return redirect(request.url)
try:
image = lamps.open_rotated_image(request.files['file'])
except FileNotFoundError:
return render_template('lamp.html',
error='Upload error: image not found')
except OSError:
return render_template('lamp.html',
error='Upload error: unable to read image file')
filename = request.files['file'].filename
filename = secure_filename(filename)
cache_filename = os.path.split(filename)[1]
# The form seems to dislike _ at the end of a field. We
# finesse that by making sure it always ends with 'lamp'
tempdir = tempfile.mkdtemp(suffix='lamp')
cache_dir = os.path.split(tempdir)[1]
tmp_file = os.path.join(tempdir, cache_filename)
print("Storing %s in %s" % (filename, tmp_file))
request.files['file'].seek(0)
request.files['file'].save(tmp_file)
resize = request.form.get('resize', 'default')
width = request.form.get('width', '90', type=int)
height = request.form.get('height', '60', type=int)
num_lamps = request.form.get('lamps', '5000', type=int)
if resize == 'default':
image = lamps.resize_image(image, default=True)
elif resize == 'lamps':
if num_lamps > MAX_LAMPS:
return render_template('lamp.html',
error='Unable to handle more than %d lamps' % MAX_LAMPS)
image = lamps.resize_image(image, lamps=num_lamps)
elif resize == 'size':
if height * width > MAX_LAMPS:
return render_template('lamp.html',
error='Unable to handle more than %d lamps' % MAX_LAMPS)
image = lamps.resize_image(image, shape=(width, height))
else:
return render_template('lamp.html',
error='Unknown resize option')
colors = request.form.get('colors', 'base')
base_black = request.form.get('base_black', 'False') == 'True'
if colors == 'expanded':
color_set = lamps.EXPANDED_LAMP_COLORS
disable_black = False
elif colors == 'dectorio':
color_set = lamps.DECTORIO_LAMP_COLORS
disable_black = False
elif colors == 'base':
color_set = lamps.BASE_COLORS
disable_black = True
if not base_black:
color_set = [x for x in color_set if x.name != 'signal-black']
else:
return render_template('lamp.html',
error='Unknown color set')
method = request.form.get('method', 'kmeans')
if method == 'kmeans':
bp, _ = lamps.convert_image_to_blueprint_kmeans(image, color_set, disable_black)
elif method == 'nearest':
bp, _ = lamps.convert_image_to_blueprint_nearest(image, color_set, disable_black)
else:
return render_template('lamp.html',
error='Unknown color reduction method')
preview_image = lamps.convert_blueprint_to_preview(bp, color_set)
f = io.BytesIO()
preview_image.save(f, format="PNG")
preview = base64.b64encode(f.getvalue())
stats = lamps.extract_blueprint_stats(bp)
return render_template('lamp.html', bp=bp,
cache_filename=cache_filename, cache_dir=cache_dir,
preview=preview.decode("utf-8"), stats=stats,
resize=resize, width=width, height=height,
num_lamps=num_lamps, colors=colors, method=method,
base_black=base_black)
return render_template('lamp.html')
if __name__ == "__main__":
#application.run()
application.run(host="0.0.0.0", port=8080)