-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
50 lines (39 loc) · 1.6 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
from flask import Flask, request, send_file, render_template, jsonify
from rembg import remove
from PIL import Image
import io
import os
import base64
app = Flask(__name__)
UPLOAD_FOLDER = 'uploads'
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg'}
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':
print("Received POST request")
if 'file' not in request.files:
print("No file part")
return jsonify({"error": "No file part"}), 400
file = request.files['file']
if file.filename == '':
print("No selected file")
return jsonify({"error": "No selected file"}), 400
if file and allowed_file(file.filename):
print(f"Processing file: {file.filename}")
input_image = Image.open(file.stream)
output_image = remove(input_image)
img_byte_arr = io.BytesIO()
output_image.save(img_byte_arr, format='PNG')
img_byte_arr.seek(0)
img_base64 = base64.b64encode(img_byte_arr.getvalue()).decode('utf-8')
print("Image processed successfully")
return jsonify({"image": img_base64})
else:
print("File not allowed")
return jsonify({"error": "File not allowed"}), 400
return render_template('upload.html')
if __name__ == '__main__':
app.run(debug=True)