Skip to content

armgit / rembg

Public Imported from GitHub
0

Rembg is a tool to remove images background

Support for sending file with POST request

Ivan Sidorovcbeffe5ccdfb
diff --git a/README.md b/README.md
index 16e97ac..a7ae344 100644
--- a/README.md
+++ b/README.md
@@ -72,6 +72,14 @@ Open your browser to
http://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 library
In `app.py`
diff --git a/src/rembg/cmd/server.py b/src/rembg/cmd/server.py
index 664892b..0318cc1 100644
--- a/src/rembg/cmd/server.py
+++ b/src/rembg/cmd/server.py
@@ -5,24 +5,40 @@ from urllib.request import urlopen
from flask import Flask, request, send_file
from waitress import serve
+import os
from ..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"}, 400
try:
return send_file(
- BytesIO(remove(urlopen(unquote_plus(url)).read(), model)),
+ BytesIO(remove(file_content, model)),
mimetype="image/png",
)
except Exception as e: