INNER CODE UNIT · Python
create_soft_mask
Tencent-Hunyuan/HunyuanPortrait · inference.py:24
def create_soft_mask(size, border_ratio=0.1):
"""
create a soft mask with edge blurring for smooth blending.
size: (width, height)
"""
w, h = size
mask = np.ones((h, w), dtype=np.float32)
# calculate the number of pixels for edge blurring
border_w = int(w * border_ratio)
border_h = int(h * border_ratio)
# horizontal direction gradient
if border_w > 0:
mask[:, :border_w] = np.linspace(0, 1, border_w)[None, :]
mask[:, -border_w:] = np.linspace(1, 0, border_w)[None, :]
# vertical direction gradient