Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add background color functionality to the remove function #414

Merged
merged 5 commits into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ The available models are:
- u2net_human_seg ([download](https://github.com/danielgatis/rembg/releases/download/v0.0.0/u2net_human_seg.onnx), [source](https://github.com/xuebinqin/U-2-Net)): A pre-trained model for human segmentation.
- u2net_cloth_seg ([download](https://github.com/danielgatis/rembg/releases/download/v0.0.0/u2net_cloth_seg.onnx), [source](https://github.com/levindabhi/cloth-segmentation)): A pre-trained model for Cloths Parsing from human portrait. Here clothes are parsed into 3 category: Upper body, Lower body and Full body.
- silueta ([download](https://github.com/danielgatis/rembg/releases/download/v0.0.0/silueta.onnx), [source](https://github.com/xuebinqin/U-2-Net/issues/295)): Same as u2net but the size is reduced to 43Mb.
- isnet-general-use ([download](https://github.com/danielgatis/rembg/releases/download/v0.0.0/isnet-general-use.onnx), [source](https://github.com/xuebinqin/U-2-Net/issues/295)): https://github.com/xuebinqin/DIS.
- isnet-general-use ([download](https://github.com/danielgatis/rembg/releases/download/v0.0.0/isnet-general-use.onnx), [source](https://github.com/xuebinqin/DIS)): A new pre-trained model for general use cases.

### Some differences between the models result

Expand Down
12 changes: 12 additions & 0 deletions rembg/bg.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ def post_process(mask: np.ndarray) -> np.ndarray:
return mask


def apply_background_color(img: PILImage, color: List[int]) -> PILImage:
r, g, b = color
colored_image = Image.new("RGBA", img.size, (r, g, b, 255))
colored_image.paste(img, mask=img)

return colored_image


def remove(
data: Union[bytes, PILImage, np.ndarray],
alpha_matting: bool = False,
Expand All @@ -114,6 +122,7 @@ def remove(
session: Optional[BaseSession] = None,
only_mask: bool = False,
post_process_mask: bool = False,
color: Optional[List[int]] = None,
) -> Union[bytes, PILImage, np.ndarray]:
if isinstance(data, PILImage):
return_type = ReturnType.PILLOW
Expand Down Expand Up @@ -161,6 +170,9 @@ def remove(
if len(cutouts) > 0:
cutout = get_concat_v_multi(cutouts)

if color is not None:
cutout = apply_background_color(cutout, color)

if ReturnType.PILLOW == return_type:
return cutout

Expand Down
17 changes: 17 additions & 0 deletions rembg/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ def main() -> None:
show_default=True,
help="post process the mask",
)
@click.option(
"-c",
"--color",
default=None,
nargs=3,
type=int,
help="Background color (R G B) to replace the removed background with",
)
@click.argument(
"input", default=(None if sys.stdin.isatty() else "-"), type=click.File("rb")
)
Expand Down Expand Up @@ -176,6 +184,15 @@ def i(model: str, input: IO, output: IO, **kwargs) -> None:
show_default=True,
help="watches a folder for changes",
)
@click.option(
"-c",
"--color",
default=None,
type=(int, int, int),
nargs=3,
metavar="R G B",
help="background color (RGB) to replace removed areas",
)
@click.argument(
"input",
type=click.Path(
Expand Down