Rembg is a tool to remove images background
Support for sending file with POST request
Ivan Sidorovcbeffe5ccdfb
diff --git a/README.md b/README.mdindex 16e97ac..a7ae344 100644--- a/README.md+++ b/README.md@@ -72,6 +72,14 @@ Open your browser tohttp://localhost:5000?url=http://image.png```+Also you can send the file as a FormData (multipart/form-data):+```+ <form action='http://localhost:5000' method='post' enctype='multipart/form-data'>+ <input type=file name=file>+ <input type=submit value=Upload>+ </form>+```+### Usage as a libraryIn `app.py`diff --git a/src/rembg/cmd/server.py b/src/rembg/cmd/server.pyindex 664892b..0318cc1 100644--- a/src/rembg/cmd/server.py+++ b/src/rembg/cmd/server.py@@ -5,24 +5,40 @@ from urllib.request import urlopenfrom flask import Flask, request, send_filefrom waitress import serve+import osfrom ..bg import remove-app = Flask(__name__)+app = Flask(__name__)[email protected]('/', methods=['GET', 'POST'])def index():+model = request.args.get("model", type=str, default="u2net")if model not in ("u2net", "u2netp"):return {"error": "invalid query param 'model'"}, 400- url = request.args.get("url", type=str)- if url is None:- return {"error": "missing query param 'url'"}, 400+ file_content = ''+ if request.method == 'POST':+ if 'file' not in request.files:+ return {"error": "missing post form param 'file'"}, 400++ file_content = request.files['file'].read();++ if request.method == 'GET':+ url = request.args.get("url", type=str)+ if url is None:+ return {"error": "missing query param 'url'"}, 400++ file_content = urlopen(unquote_plus(url)).read();++ if file_content == '':+ return {"error": "File content is empty"}, 400try:return send_file(- BytesIO(remove(urlopen(unquote_plus(url)).read(), model)),+ BytesIO(remove(file_content, model)),mimetype="image/png",)except Exception as e: