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
评论列表
文章目录