def save_result(alpha, path, im_path, trimap=None, fg_estimate=True, fg=None):
    """
    Save alpha and rgba.

    Args:
        alpha (numpy.ndarray): The value of alpha should in [0, 255], shape should be [h,w].
        path (str): The save path
        im_path (str): The original image path.
        trimap (str, optional): The trimap if provided. Default: None.
        fg_estimate (bool, optional): Whether to estimate the foreground, Default: True.
        fg (numpy.ndarray, optional): The foreground, if provided, fg_estimate is invalid. Default: None.
    """
    dirname = os.path.dirname(path)
    if not os.path.exists(dirname):
        os.makedirs(dirname)
    basename = os.path.basename(path)
    name = os.path.splitext(basename)[0]
    alpha_save_path = os.path.join(dirname, name + '_alpha.png')
    rgba_save_path = os.path.join(dirname, name + '_rgba.png')

    # save alpha matte
    if trimap is not None:
        trimap = cv2.imread(trimap, 0)
        alpha[trimap == 0] = 0
        alpha[trimap == 255] = 255
    alpha = (alpha).astype('uint8')
    cv2.imwrite(alpha_save_path, alpha)

    # save rgba
    im = cv2.imread(im_path)
    if fg is None:
        if fg_estimate:
            fg = estimate_foreground_ml(im / 255.0, alpha / 255.0) * 255
        else:
            fg = im
    fg = fg.astype('uint8')
    alpha = alpha[:, :, np.newaxis]
    rgba = np.concatenate((fg, alpha), axis=-1)
    cv2.imwrite(rgba_save_path, rgba)

    return fg

    Save alpha and rgba.

    Args:
        alpha (numpy.ndarray): The value of alpha should in [0, 255], shape should be [h,w].
        path (str): The save path
        im_path (str): The original image path.
        trimap (str, optional): The trimap if provided. Default: None.
        fg_estimate (bool, optional): Whether to estimate the foreground, Default: True.
        fg (numpy.ndarray, optional): The foreground, if provided, fg_estimate is invalid. Default: None.
    
Matting/ppmatting/core/predict.py