-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
111 lines (75 loc) · 2.92 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
import logging
from flask import Flask, request, jsonify, send_file
from flask_httpauth import HTTPBasicAuth
import os
import logging
from rembg import remove
import time
ALLOWED_EXTENSIONS = {'jpg', 'jpeg', 'png'}
def environment():
return os.environ['APP_ENV']
def production():
return environment() == "production"
def development():
return not production()
def log_filename():
return "production.log" if production() else "development.log"
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
def file_name_without_ext(filename):
return os.path.splitext(filename)[0]
def transform_file(input, output, filename):
input += filename
output += file_name_without_ext(filename) + ".png"
with open(input, "rb") as i:
with open(output, "wb") as o:
input = i.read()
output = remove(input)
o.write(output)
if production():
logging.basicConfig(filename=log_filename(), level=logging.DEBUG)
app = Flask(__name__)
auth = HTTPBasicAuth()
@auth.verify_password
def verify_password(username, password):
if username == os.environ['USERNAME'] and os.environ['PASSWORD'] == password:
return True
return False
@app.route("/remove_background", methods=["POST"])
@auth.login_required
def transform():
if 'image' not in request.files:
return jsonify({'error': 'No image file in the request'}), 400
file = request.files['image']
if not allowed_file(file.filename):
return jsonify({'error': 'Invalid image file type'}), 400
if not os.path.exists('uploads'):
os.makedirs('uploads')
if not os.path.exists('uploads/edited_file'):
os.makedirs('uploads/edited_file')
if not os.path.exists('uploads/original_file'):
os.makedirs('original_file')
timestamp = str(int(time.time() * 1000)) + "-"
file_name = timestamp + file.filename
file.save(os.path.join('uploads/original_file', file_name))
transform_file('uploads/original_file/', 'uploads/edited_file/', file_name)
return send_file('uploads/edited_file/' + file_name_without_ext(file_name) + ".png", mimetype='image/png')
@app.route("/get_image", methods=["GET"])
@auth.login_required
def get_image():
filename = request.args.get('filename')
original = request.args.get('original') or False
if not filename:
return {'error': 'Filename parameter is required.'}, 400
if not allowed_file(filename):
return jsonify({'error': 'Invalid image file type'}), 400
if original:
file_path = os.path.join('uploads/original_file', filename)
else:
file_path = os.path.join('uploads/edited_file', filename)
try:
return send_file(file_path, mimetype='image/png')
except FileNotFoundError:
return jsonify({'error': 'File not found.'}), 404
if __name__ == "__main__":
app.run(debug=development(), port=8000, host="0.0.0.0")