Skip to content

Commit 68595bd

Browse files
authored
Merge pull request #519 from DenisKochetov/partcrafter-rembg
add white background
2 parents cf913d2 + ca83e19 commit 68595bd

2 files changed

Lines changed: 11 additions & 4 deletions

File tree

Gen_3D_Modules/Hunyuan3D_2_1/hy3dshape/hy3dshape/rembg.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@
1515
from PIL import Image
1616
from rembg import remove, new_session
1717
import numpy as np
18+
from typing import Optional, Tuple
1819

1920

2021
class BackgroundRemover():
2122
def __init__(self, model_name='u2net'):
2223
self.session = new_session(model_name)
2324

24-
def __call__(self, image: Image.Image):
25+
def __call__(self, image: Image.Image, background_color: Optional[Tuple[int, int, int]] = None):
2526
if image.mode in ('RGBA', 'LA') or image.mode != 'RGB':
2627
background = Image.new('RGB', image.size, (255, 255, 255))
2728
if image.mode in ('RGBA', 'LA'):
@@ -34,5 +35,11 @@ def __call__(self, image: Image.Image):
3435

3536
if output.mode != 'RGBA':
3637
output = output.convert('RGBA')
37-
38-
return output
38+
39+
if background_color is not None:
40+
# Add background color to the output image
41+
filled = Image.new("RGB", output.size, background_color)
42+
filled.paste(output, mask=output.split()[-1])
43+
return filled
44+
45+
return output

nodes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5889,7 +5889,7 @@ def generate(self, partcrafter_pipe, image, num_parts, seed, num_tokens, num_inf
58895889
rmbg_worker = BackgroundRemover_2_1()
58905890
if pil_image.mode == "RGBA":
58915891
pil_image = pil_image.convert('RGB')
5892-
pil_image = rmbg_worker(pil_image)
5892+
pil_image = rmbg_worker(pil_image, background_color=(255, 255, 255))
58935893
del rmbg_worker
58945894

58955895
# Set sampling version

0 commit comments

Comments
 (0)