INNER CODE UNIT · Python
paste_back_frame
Tencent-Hunyuan/HunyuanPortrait · gradio_app.py:146
def paste_back_frame(original_img, generated_crop, crop_bbox, mask=None):
"""paste the generated cropped frame back to the original image"""
x1, y1, x2, y2 = crop_bbox
target_w = x2 - x1
target_h = y2 - y1
generated_resized = cv2.resize(generated_crop, (target_w, target_h))
if mask is None:
mask = create_soft_mask((target_w, target_h))
background_region = original_img[y1:y2, x1:x2].astype(np.float32) / 255.0
foreground_region = generated_resized.astype(np.float32) / 255.0
blended_region = foreground_region * mask + background_region * (1 - mask)
result_img = original_img.copy()
result_img[y1:y2, x1:x2] = (blended_region * 255).astype(np.uint8)