commit 333b2cf3a927d6f3f94fa48d3c706887774d6d12 Author: Leonard Excoffier Date: Wed May 1 12:31:54 2024 -0400 Initial commit diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..f16d973 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,13 @@ +FROM python:latest + +WORKDIR /app + +COPY requirements.txt . + +RUN pip install --no-cache-dir -r requirements.txt + +COPY . . + +EXPOSE 5124 + +CMD ["python", "app.py"] \ No newline at end of file diff --git a/app.py b/app.py new file mode 100644 index 0000000..de12002 --- /dev/null +++ b/app.py @@ -0,0 +1,43 @@ +from flask import Flask, render_template, request, send_file, flash, redirect, url_for +from rembg import remove +from PIL import Image +from io import BytesIO + +app = Flask(__name__) + +# Update allowed extensions to include additional file types +ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif', 'bmp', 'tiff', 'webp'} +# Set max content length to 1GB (expressed in bytes) +app.config['MAX_CONTENT_LENGTH'] = 1 * 1024 * 1024 * 1024 # 1 GB limit + +def file_allowed(filename): + """ + Check if the file has an allowed extension. + """ + return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS + +@app.route('/', methods=['GET', 'POST']) +def upload_file(): + """ + Handle file upload via POST, process image to remove background, and return the processed image. + """ + if request.method == 'POST': + file = request.files.get('file', None) + if file and file_allowed(file.filename): + try: + input_image = Image.open(file.stream) + output_image = remove(input_image, post_process_mask=True) + img_io = BytesIO() + output_image.save(img_io, 'PNG') + img_io.seek(0) + return send_file(img_io, mimetype='image/png', as_attachment=True, download_name='processed_image.png') + except Exception as e: + flash('Failed to process the image. Error: {}'.format(e), 'error') + return redirect(url_for('upload_file')) + else: + flash('Please upload a valid image file (PNG, JPG, JPEG, GIF, BMP, TIFF, WEBP).', 'error') + return redirect(url_for('upload_file')) + return render_template('index.html') + +if __name__ == '__main__': + app.run(host='0.0.0.0', debug=True, port=5124) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..10ac15b --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +flask +rembg +pillow \ No newline at end of file diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..aa4350d --- /dev/null +++ b/templates/index.html @@ -0,0 +1,12 @@ + + + + Background Remover + + +
+ + +
+ + \ No newline at end of file