forked from SubhrajitPrusty/wallgen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
235 lines (187 loc) · 6.2 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import os
import time
from wallgen import *
from PIL import Image
from skimage.filters import sobel
from gevent.pywsgi import WSGIServer
from werkzeug.utils import secure_filename
from skimage import color, img_as_ubyte, io
from flask import Flask, request, send_file, render_template, redirect, url_for
UPLOAD_FOLDER = os.path.join("static","upload")
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg'])
app = Flask(__name__, static_url_path="/static")
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['MAX_CONTENT_LENGTH'] = 5 * 1024 * 1024
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route("/", methods=['GET'])
def index():
return render_template("home.html")
@app.route("/poly", methods=['GET','POST'])
def poly():
if request.method == 'POST':
# get data
side = int(request.form.get('side'))
np = int(request.form.get('np'))
outline = request.form.get('outline')
bgtype = request.form.get('bgtype')
swirl = request.form.get('swirl')
error = None
if side > 5000 or side < 100:
error = "WARNING: Image too large OR Image too small"
if np < 10 or np > 10001:
error = "WARNING: Too less points OR too many points"
fname = "wall-{}.png".format(int(time.time()))
fpath = 'static/images/'+fname
shift = side//10
nside = side + shift*2 # increase size to prevent underflow
img = random_gradient(nside)
if bgtype == "nbyn":
img = NbyNGradient(nside)
elif bgtype == "customColors":
nColors = request.form.get('nColors')
colors = []
for i in range(int(nColors)):
colors.append(request.form.get('rgb'+str(i+1)))
try:
colors = [tuple(bytes.fromhex(x[1:])) for x in colors]
except Exception as e:
print(e)
error = "ERROR: Invalid color hex"
img = nGradient(nside, *colors)
if error != None:
print(error)
return render_template('error.html', context=error)
if outline:
outline = tuple(bytes.fromhex("#2c2c2c"[1:]))
else:
outline = None
if swirl:
img = swirl_image(img)
pts = genPoints(np, nside, nside)
img = genPoly(side, side, img, pts, shift, shift, outl=outline)
# print(fpath)
img.save(fpath)
imgurl = url_for('static',filename='images/'+fname)
return render_template("download.html", context=imgurl, home="poly")
else:
return render_template('poly.html')
@app.route("/shape", methods=['GET','POST'])
def shape():
if request.method == 'POST':
side = int(request.form.get('side'))
outline = request.form.get('outline')
bgtype = request.form.get('bgtype')
swirl = request.form.get('swirl')
shape = request.form.get('shape')
error = None
if side > 5000 or side < 100:
error = "WARNING: Image too large OR Image too small"
fname = "wall-{}.png".format(int(time.time()))
fpath = 'static/images/'+fname
img = random_gradient(side)
if bgtype == "nbyn":
img = NbyNGradient(side)
elif bgtype == "customColors":
nColors = request.form.get('nColors')
colors = []
for i in range(int(nColors)):
colors.append(request.form.get('rgb'+str(i+1)))
try:
colors = [tuple(bytes.fromhex(x[1:])) for x in colors]
except Exception as e:
print(e)
error = "ERROR: Invalid color hex"
img = nGradient(side, *colors)
if error != None:
print(error)
return render_template('error.html', context=error)
if outline:
outline = tuple(bytes.fromhex("#2c2c2c"[1:]))
else:
outline = None
if swirl:
img = swirl_image(img)
if shape == 'hexagon':
img = genHexagon(side, side, img, outline)
elif shape == 'squares':
img = genSquares(side, side, img, outline)
elif shape == 'diamond':
img = genDiamond(side, side, img, outline)
elif shape == 'triangle':
img = genTriangle(side, side, img, outline)
elif shape == 'isometric':
img = genIsometric(side, side, img, outline)
# print(fpath)
img.save(fpath)
imgurl = url_for('static',filename='images/'+fname)
return render_template("download.html", context=imgurl, home="shape")
else:
return render_template('shape.html')
@app.route("/pic", methods=['GET', 'POST'])
def pic():
if request.method == 'POST':
# print(request.files)
# print(request.form)
if 'image' not in request.files:
error = "No file part"
return render_template("error.html", context=error)
else:
file = request.files['image']
# print(file.filename)
# print(len(file.filename))
if len(file.filename) < 1:
error = "No file selected"
return render_template("error.html", context=error)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
ufpath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(ufpath)
np = request.form.get('np')
outline = request.form.get('outline')
smart = request.form.get('smart')
if np or smart:
og_img = Image.open(ufpath)
width = og_img.width
height = og_img.height
if min(height, width) > 1080:
scale = min(height, width)//1080
else:
scale = 1
img = og_img.resize((width//scale, height//scale), resample=Image.BICUBIC)
width = img.width
height = img.height
wshift = width//100
hshift = height//100
n_width = width + 2*wshift
n_height = height + 2*height
if outline:
outline = tuple(bytes.fromhex("#2c2c2c"[1:]))
else:
outline = None
if smart:
ski_img = io.imread(ufpath, True)
gray_img = color.rgb2gray(ski_img)
pts = genSmartPoints(gray_img)
else:
pts = genPoints(int(np), n_width, n_height)
img = genPoly(img.width, img.height, img, pts, wshift, hshift, outline, pic=True)
fname = "wall-{}.png".format(int(time.time()))
fpath = 'static/images/'+fname
# print(fpath)
img.save(fpath)
imgurl = url_for('static',filename='images/'+fname)
return render_template("download.html", context=imgurl, home="pic")
else:
error = "Invalid input, try again"
return render_template("error.html", context=error)
else:
error = "filetype not allowed"
return render_template("error.html", context=error)
else:
return render_template("pic.html")
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
http_server = WSGIServer(('',port),app)
http_server.serve_forever()