forked from LucindeAI/rbg
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
80 lines (63 loc) · 2.33 KB
/
Copy pathmain.py
File metadata and controls
80 lines (63 loc) · 2.33 KB
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
import os
import secrets
import string
from flask import (
Flask,
flash,
redirect,
render_template,
request,
send_from_directory,
url_for,
)
from werkzeug.utils import secure_filename
from rembg import remove
UPLOAD_FOLDER = "./uploads"
ALLOWED_EXTENSIONS = {"png", "jpg", "jpeg", "gif"}
special_chars = "_!/?"
length = 10
app = Flask(__name__)
app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER
def allowed_file(filename):
return "." in filename and filename.rsplit(".", 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route("/", methods=["GET", "POST"])
def upload_file():
if request.method == "POST":
if "file" not in request.files:
flash("No file part")
return redirect(request.url)
file = request.files["file"]
if file.filename == "":
flash("No selected file")
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config["UPLOAD_FOLDER"], filename))
folder = "uploads/"
input_image = filename
# generate a random name for result image
chars = string.ascii_letters + string.digits
while True:
output_image = ''.join(secrets.choice(chars)
for i in range(10)) + '.png'
if (any(c.islower() for c in output_image)
and any(c.isupper() for c in output_image)
and sum(c.isdigit() for c in output_image) >= 3):
break
input_path = folder + input_image
output_path = folder + output_image
with open(input_path, "rb") as i:
with open(output_path, "wb") as o:
input = i.read()
output = remove(input)
o.write(output)
o.close()
return redirect(
url_for("uploaded_file", filename=secure_filename(output_image))
)
return render_template("index.html")
@app.route("/uploads/<filename>")
def uploaded_file(filename):
return send_from_directory(app.config["UPLOAD_FOLDER"], filename)
if __name__ == "__main__":
app.run(debug=False, host="0.0.0.0", port=int(os.environ.get("PORT", 8080)))