flk / app.py
Geek7's picture
Update app.py
ff10734 verified
raw
history blame
1.06 kB
from flask import Flask, jsonify, request, send_file
from flask_cors import CORS
from rembg import remove
from PIL import Image
import io
# Initialize the Flask app
app = Flask(__name__)
CORS(app) # Enable CORS if needed
@app.route('/')
def home():
return "Welcome to the Image Background Remover!" # Basic home response
@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 uploaded image
output_bytes = remove(input_image) # Process the image with rembg
# Convert the output bytes back into a PIL 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')
# Add this block to make sure your app runs when called
if __name__ == "__main__":
app.run # Run directly if needed for testing