Skip to content

armgit / rembg

Public Imported from GitHub
0

Rembg is a tool to remove images background

add resizing image

DmitryKazakevich84be65c551bc
diff --git a/src/rembg/bg.py b/src/rembg/bg.py
index 3a14d14..56c9cb2 100644
--- a/src/rembg/bg.py
+++ b/src/rembg/bg.py
@@ -78,8 +78,17 @@ def get_model(model_name):
return detect.load_model(model_name="u2net")
+def resize_image(img, width, height):
+ original_width, original_height = img.size
+ width = original_width if width is None else width
+ height = original_height if height is None else height
+ return img.resize((width, height)) if original_width != width or original_height != height else img
+
+
def remove(
data,
+ width,
+ height,
model_name="u2net",
alpha_matting=False,
alpha_matting_foreground_threshold=240,
@@ -89,6 +98,8 @@ def remove(
):
model = get_model(model_name)
img = Image.open(io.BytesIO(data)).convert("RGB")
+ if width is not None or height is not None:
+ img = resize_image(img, width, height)
mask = detect.predict(model, np.array(img)).convert("L")
if alpha_matting:
diff --git a/src/rembg/cmd/server.py b/src/rembg/cmd/server.py
index 61a048c..96ace70 100644
--- a/src/rembg/cmd/server.py
+++ b/src/rembg/cmd/server.py
@@ -42,6 +42,8 @@ def index():
ab = request.values.get("ab", type=int, default=10)
ae = request.values.get("ae", type=int, default=10)
az = request.values.get("az", type=int, default=1000)
+ width = request.args.get("width", type=int)
+ height = request.args.get("height", type=int)
model = request.args.get("model", type=str, default="u2net")
model_path = os.environ.get(
@@ -65,6 +67,8 @@ def index():
BytesIO(
remove(
file_content,
+ width,
+ height,
model_name=model,
alpha_matting=alpha_matting,
alpha_matting_foreground_threshold=af,