pip install rembg opencv-python pillow flask
from rembg import remove
from PIL import Image
import cv2
import numpy as np
def remove_background(input_path, output_path):
# Open the image
input_image = Image.open(input_path)
# Remove the background
output_image = remove(input_image)
# Save the output image
output_image.save(output_path)
print(f"Background removed and saved to {output_path}")
# Example usage
if __name__ == "__main__":
input_image_path = "input.jpg" # Replace with your input image path
output_image_path = "output.png" # Replace with your desired output path
remove_background(input_image_path, output_image_path)
Background Remover
{% endif %}
from flask import Flask, render_template, request, redirect, url_for
import os
from rembg import remove
from PIL import Image
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = 'static/uploads'
app.config['OUTPUT_FOLDER'] = 'static/outputs'
if not os.path.exists(app.config['UPLOAD_FOLDER']):
os.makedirs(app.config['UPLOAD_FOLDER'])
if not os.path.exists(app.config['OUTPUT_FOLDER']):
os.makedirs(app.config['OUTPUT_FOLDER'])
@app.route('/')
def index():
return render_template('index.html')
@app.route('/upload', methods=['POST'])
def upload():
if 'image' not in request.files:
return redirect(url_for('index'))
file = request.files['image']
if file.filename == '':
return redirect(url_for('index'))
# Save the uploaded file
input_path = os.path.join(app.config['UPLOAD_FOLDER'], file.filename)
file.save(input_path)
# Remove the background
output_path = os.path.join(app.config['OUTPUT_FOLDER'], 'output.png')
input_image = Image.open(input_path)
output_image = remove(input_image)
output_image.save(output_path)
return render_template('index.html', output_image=output_path)
if __name__ == '__main__':
app.run(debug=True)
python app.py
Comments
Post a Comment