-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
64 lines (41 loc) · 1.64 KB
/
main.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
from flask import Flask, render_template, request, redirect, url_for
import os
from PIL import Image
from rembg import remove
app = Flask(__name__)
@app.route('/')
def home():
try:
#We delete all the existing input and output images (to save space :D)
input_dir_path = './static/input/'
output_dir_path = './static/output/'
for x in os.listdir(input_dir_path):
filename = os.path.join(input_dir_path,x)
os.remove(filename)
for x in os.listdir(output_dir_path):
filename = os.path.join(output_dir_path,x)
os.remove(filename)
except Exception as e:
print(f'Could not delete: {e}')
finally:
return render_template('home.html')
@app.route('/output', methods=['POST'])
def output():
img = request.files['img']
if img:
# Save the uploaded image
img_path = os.path.join('static/input', img.filename)
img.save(img_path)
# Process the uploaded image using rembg
try:
input_image = Image.open(img_path) # Assuming you have PIL installed
output_image = remove(input_image)
output_path = os.path.join('static/output', img.filename)
output_image.save(output_path)
except Exception as e:
print(f"Error Occurred: {e}")
return render_template('home.html',error="Error: Please select a different image")
return render_template('output.html', input_image=img_path, output_image=output_path)
return redirect(url_for('home'))
if __name__ == "__main__":
app.run(debug=True)