File size: 960 Bytes
f66a250
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from flask import Flask, jsonify, request, send_file
from flask_cors import CORS
from rembg import remove
from PIL import Image
import io

app = Flask(__name__)
CORS(app)  # Enable CORS for all routes

@app.route('/remove_background', methods=['POST'])
def remove_background():
    if 'image' not in request.files:
        return jsonify({"error": "No image provided"}), 400

    input_image = request.files['image'].read()  # Read the image file

    # Apply background removal using rembg
    output_bytes = remove(input_image)

    # Convert the output bytes back into a PIL image
    output_image = Image.open(io.BytesIO(output_bytes))

    # Save the processed image to a BytesIO object
    img_byte_arr = io.BytesIO()
    output_image.save(img_byte_arr, format='PNG')
    img_byte_arr.seek(0)

    # Return the image as a response
    return send_file(img_byte_arr, mimetype='image/png')

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=7860)