Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,33 @@
|
|
1 |
-
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
|
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
#
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, jsonify, request, send_file
|
2 |
+
from flask_cors import CORS
|
3 |
+
from rembg import remove
|
4 |
+
from PIL import Image
|
5 |
+
import io
|
6 |
|
7 |
+
# Initialize the Flask app
|
8 |
+
myapp = Flask(__name__)
|
9 |
+
CORS(myapp) # Enable CORS if needed
|
10 |
|
11 |
+
@myapp.route('/')
|
12 |
+
def home():
|
13 |
+
return "Welcome to the Image Background Remover!" # Basic home response
|
14 |
+
|
15 |
+
@myapp.route('/remove_background', methods=['POST'])
|
16 |
+
def remove_background():
|
17 |
+
if 'image' not in request.files:
|
18 |
+
return jsonify({"error": "No image provided"}), 400
|
19 |
+
|
20 |
+
input_image = request.files['image'].read() # Read the uploaded image
|
21 |
+
output_bytes = remove(input_image) # Process the image with rembg
|
22 |
|
23 |
+
# Convert the output bytes back into a PIL image
|
24 |
+
output_image = Image.open(io.BytesIO(output_bytes))
|
25 |
+
img_byte_arr = io.BytesIO()
|
26 |
+
output_image.save(img_byte_arr, format='PNG')
|
27 |
+
img_byte_arr.seek(0)
|
28 |
+
|
29 |
+
return send_file(img_byte_arr, mimetype='image/png')
|
30 |
+
|
31 |
+
# Add this block to make sure your app runs when called
|
32 |
+
if __name__ == "__main__":
|
33 |
+
myapp.run(host='0.0.0.0', port=7860) # Run directly if needed for testing
|