-
Notifications
You must be signed in to change notification settings - Fork 1
/
glitch_app.py
268 lines (223 loc) · 7.17 KB
/
glitch_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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import os
import os.path as osp
import uuid
import glob
import shutil
import hashlib
import sass
from flask import (
Flask,
flash,
request,
redirect,
url_for,
render_template,
send_file,
jsonify,
)
from werkzeug.utils import secure_filename
from itsdangerous import URLSafeSerializer
from sassutils.wsgi import SassMiddleware
from random import sample
import requests
from glitch.apps import glitch_image, glitch_video
signer = URLSafeSerializer("super-secret")
UPLOAD_FOLDER = "uploads"
STATIC_FOLDER = "static"
ASSETS_FOLDER = "assets"
ALLOWED_EXTENSIONS = {"image": ["png", "jpg", "jpeg"], "video": ["mov", "mp4", "ts"]}
COMMON_OPTIONS = {
"noise_intensity": {
"label": "Noise Intensity",
"min": 0,
"max": 1,
"step": 0.01,
"default": 0.5,
"type": float,
},
"noise_amount": {
"label": "Noise Amount",
"min": 0,
"max": 1,
"step": 0.01,
"default": 0.5,
"type": float,
},
"channels_movement": {
"label": "Channels Movement",
"min": 0,
"max": 1,
"step": 0.01,
"default": 0.5,
"type": float,
},
"block_size": {
"label": "Block Size",
"min": 0,
"max": 1,
"step": 0.01,
"default": 0.5,
"type": float,
},
"block_count": {
"label": "Block Count",
"min": 0,
"max": 100,
"step": 1,
"default": 15,
"type": int,
},
}
IMAGE_OPTIONS = {**COMMON_OPTIONS}
VIDEO_OPTIONS = {
"min_effect_length": {
"label": "Minimum effect duration (in frames)",
"min": 1,
"max": 10,
"step": 1,
"default": 1,
"type": int,
},
"max_effect_length": {
"label": "Maximum effect duration (in frames)",
"min": 5,
"max": 30,
"step": 1,
"default": 15,
"type": int,
},
"scanlines_intensity": {
"label": "Scanlines intensity",
"min": 0,
"max": 1,
"step": 0.01,
"default": 0,
"type": float,
},
**COMMON_OPTIONS,
}
app = Flask(__name__, static_folder=STATIC_FOLDER)
app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER
app.config["SECRET_KEY"] = "1234asdf"
# create dirs if needed
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
os.makedirs(STATIC_FOLDER, exist_ok=True)
for media_type in ALLOWED_EXTENSIONS:
os.makedirs(os.path.join(UPLOAD_FOLDER, media_type), exist_ok=True)
os.makedirs(os.path.join(STATIC_FOLDER, media_type), exist_ok=True)
# Compile sass
os.makedirs(f"{STATIC_FOLDER}/css", exist_ok=True)
app.wsgi_app = SassMiddleware(
app.wsgi_app, {"glitch_app": ("assets/scss", "static/css", "/static/css")}
)
def allowed_file(filename, filetype):
return "." in filename and file_extension(filename) in ALLOWED_EXTENSIONS[filetype]
def file_extension(filename):
return filename.rsplit(".", 1)[1].lower()
def get_file_type(filename):
ext = file_extension(filename)
for ftype in ALLOWED_EXTENSIONS:
if ext in ALLOWED_EXTENSIONS[ftype]:
return ftype
return "Unknown"
def hash_file(filename: str) -> str:
with open(filename, "rb") as f:
return hashlib.md5(f.read()).hexdigest()
def get_options(file_type, request) -> object:
opt = IMAGE_OPTIONS if file_type == "image" else VIDEO_OPTIONS
return {key: opt[key]["type"](request.args.get(key)) for key in opt.keys()}
@app.route("/glitch/<string:file_type>", methods=["GET", "POST"])
def glitch(file_type):
""" glitch an image or a video.
If it is a POST request, the filename is in the requests.files """
options = IMAGE_OPTIONS if file_type == "image" else VIDEO_OPTIONS
params = {
key: options[key]["type"](options[key]["default"]) for key in options.keys()
}
if request.method == "POST":
reuse_last_file = False
file = request.files.get("file", "")
if file == "" or file.filename == "":
if request.form["filename"]:
reuse_last_file = True
else:
flash("No selected file")
return redirect(request.url)
params = {
key: options[key]["type"](request.form[key]) for key in options.keys()
}
print(f"Submitted params: {params}")
if not reuse_last_file:
if allowed_file(file.filename, file_type):
filename = secure_filename(file.filename)
filepath = os.path.join(
app.config["UPLOAD_FOLDER"], file_type, filename
)
file.save(filepath)
else:
flash("Invalid filetype")
return redirect(request.url)
else:
fname = request.form["filename"]
file_hash = os.path.basename(fname).split("_")[0]
filepath = osp.join(
STATIC_FOLDER, file_type, file_hash, f"original.{file_extension(fname)}"
)
extension = file_extension(filepath)
file_hash = hash_file(filepath)
previously_glitched = osp.exists(osp.join(STATIC_FOLDER, file_type, file_hash))
if not previously_glitched:
os.makedirs(osp.join(STATIC_FOLDER, file_type, file_hash))
new_filepath = osp.join(
STATIC_FOLDER, file_type, file_hash, f"original.{extension}"
)
shutil.copy(filepath, new_filepath)
filepath = new_filepath
other_glitches = []
else:
# glitch files are postfixed with -glitch-XX.ext
other_glitches = glob.glob(
osp.join(STATIC_FOLDER, file_type, file_hash, "*_glitch_*")
)
current_glitch_num = len(other_glitches)
glitched_fname = f"{file_hash}_glitch_{current_glitch_num}.{extension}"
glitched_filepath = osp.join(
STATIC_FOLDER, file_type, file_hash, glitched_fname
)
glitched_fname = osp.join(file_type, file_hash, glitched_fname)
if file_type == "image":
glitch_image(filepath, glitched_filepath, **params)
elif file_type == "video":
glitch_video(filepath, glitched_filepath, **params)
else:
glitched_fname = ""
other_glitches = []
return render_template(
"glitch.html",
allowed_extensions=ALLOWED_EXTENSIONS,
options=options,
parameters=params,
glitched_fname=glitched_fname,
other_glitches=other_glitches,
file_type=file_type,
)
NUM_DISPLAYED_GLITCHES = 6
@app.route("/", methods=["GET"])
def home():
images = glob.glob(osp.join(STATIC_FOLDER, "image", "*", "*_glitch_*"))
if images:
images = sample(images, min(NUM_DISPLAYED_GLITCHES, len(images)))
videos = glob.glob(osp.join(STATIC_FOLDER, "video", "*", "*_glitch_*"))
if videos:
videos = sample(videos, min(NUM_DISPLAYED_GLITCHES, len(videos)))
return render_template(
"home.html",
allowed_extensions=ALLOWED_EXTENSIONS,
images=images,
videos=videos,
)
@app.route("/health_check", methods=["GET"])
def health_check():
return jsonify({"state": "running"})
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000)