blending.py 文件源码

python
阅读 53 收藏 0 点赞 0 评论 0

项目:uchroma 作者: cyanogen 项目源码 文件源码
def blend(img_in: np.ndarray, img_layer: np.ndarray, blend_op: None, opacity: float=1.0):
    # sanity check of inputs
    assert img_in.dtype == np.float, 'Input variable img_in should be of numpy.float type.'
    assert img_layer.dtype == np.float, 'Input variable img_layer should be of numpy.float type.'
    assert img_in.shape[2] == 4, 'Input variable img_in should be of shape [:, :,4].'
    assert img_layer.shape[2] == 4, 'Input variable img_layer should be of shape [:, :,4].'
    assert 0.0 <= opacity <= 1.0, 'Opacity needs to be between 0.0 and 1.0.'

    ratio = _compose_alpha(img_in, img_layer, opacity)

    if blend_op is None:
        blend_op = BlendOp.screen
    elif isinstance(blend_op, str):
        if hasattr(BlendOp, blend_op):
            blend_op = getattr(BlendOp, blend_op)
        else:
            raise ValueError('Invalid blend mode: %s' % blend_op)

    comp = blend_op(img_in, img_layer)

    ratio_rs = np.reshape(np.repeat(ratio, 3), [comp.shape[0], comp.shape[1], comp.shape[2]])
    img_out = comp*ratio_rs + img_in[:, :, :3] * (1.0-ratio_rs)
    img_out = np.nan_to_num(np.dstack((img_out, img_in[:, :, 3])))  # add alpha channel and replace nans
    return img_out
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号