| from flask import Flask, jsonify, request, send_file,render_template |
| from flask_cors import CORS |
| from rembg import remove |
| from PIL import Image |
| import io |
|
|
| |
| myapp = Flask(__name__) |
| CORS(myapp) |
|
|
| @myapp.route('/') |
| def home(): |
| return render_template('index.html') |
|
|
| @myapp.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() |
| output_bytes = remove(input_image) |
|
|
| |
| output_image = Image.open(io.BytesIO(output_bytes)) |
| img_byte_arr = io.BytesIO() |
| output_image.save(img_byte_arr, format='PNG') |
| img_byte_arr.seek(0) |
|
|
| return send_file(img_byte_arr, mimetype='image/png') |
|
|
| |
| if __name__ == "__main__": |
| myapp.run(host='0.0.0.0', port=7860) |