import numpy as np def assert_image_format(image, fcn_name: str, arg_name: str, force_alpha: bool = True): if not isinstance(image, np.ndarray): err_msg = 'The blend_modes function "{fcn_name}" received a value of type "{var_type}" for its argument ' \ '"{arg_name}". The function however expects a value of type "np.ndarray" for this argument. Please ' \ 'supply a variable of type np.ndarray to the "{arg_name}" argument.' \ .format(fcn_name=fcn_name, arg_name=arg_name, var_type=str(type(image).__name__)) raise TypeError(err_msg) if not image.dtype.kind == 'f': err_msg = 'The blend_modes function "{fcn_name}" received a numpy array of dtype (data type) kind ' \ '"{var_kind}" for its argument "{arg_name}". The function however expects a numpy array of the ' \ 'data type kind "f" (floating-point) for this argument. Please supply a numpy array with the data ' \ 'type kind "f" (floating-point) to the "{arg_name}" argument.' \ .format(fcn_name=fcn_name, arg_name=arg_name, var_kind=str(image.dtype.kind)) raise TypeError(err_msg) if not len(image.shape) == 3: err_msg = 'The blend_modes function "{fcn_name}" received a {n_dim}-dimensional numpy array for its argument ' \ '"{arg_name}". The function however expects a 3-dimensional array for this argument in the shape ' \ '(height x width x R/G/B/A layers). Please supply a 3-dimensional numpy array with that shape to ' \ 'the "{arg_name}" argument.' \ .format(fcn_name=fcn_name, arg_name=arg_name, n_dim=str(len(image.shape))) raise TypeError(err_msg) if force_alpha and not image.shape[2] == 4: err_msg = 'The blend_modes function "{fcn_name}" received a numpy array with {n_layers} layers for its ' \ 'argument "{arg_name}". The function however expects a 4-layer array representing red, green, ' \ 'blue, and alpha channel for this argument. Please supply a numpy array that includes all 4 layers ' \ 'to the "{arg_name}" argument.' \ .format(fcn_name=fcn_name, arg_name=arg_name, n_layers=str(image.shape[2])) raise TypeError(err_msg) def assert_opacity(opacity, fcn_name: str, arg_name: str = 'opacity'): if not isinstance(opacity, float) and not isinstance(opacity, int): err_msg = 'The blend_modes function "{fcn_name}" received a variable of type "{var_type}" for its argument ' \ '"{arg_name}". The function however expects the value passed to "{arg_name}" to be of type ' \ '"float". Please pass a variable of type "float" to the "{arg_name}" argument of function ' \ '"{fcn_name}".' \ .format(fcn_name=fcn_name, arg_name=arg_name, var_type=str(type(opacity).__name__)) raise TypeError(err_msg) if not 0.0 <= opacity <= 1.0: err_msg = 'The blend_modes function "{fcn_name}" received the value "{val}" for its argument "{arg_name}". ' \ 'The function however expects that the value for "{arg_name}" is inside the range 0.0 <= x <= 1.0. ' \ 'Please pass a variable in that range to the "{arg_name}" argument of function "{fcn_name}".' \ .format(fcn_name=fcn_name, arg_name=arg_name, val=str(opacity)) raise ValueError(err_msg) def _compose_alpha(img_in, img_layer, opacity): comp_alpha = np.minimum(img_in[:, :, 3], img_layer[:, :, 3]) * opacity new_alpha = img_in[:, :, 3] + (1.0 - img_in[:, :, 3]) * comp_alpha np.seterr(divide='ignore', invalid='ignore') ratio = comp_alpha / new_alpha ratio[ratio == np.nan] = 0.0 return ratio def create_hard_light_layover(img_in, img_layer, opacity, disable_type_checks: bool = False): if not disable_type_checks: _fcn_name = 'hard_light' assert_image_format(img_in, _fcn_name, 'img_in') assert_image_format(img_layer, _fcn_name, 'img_layer') assert_opacity(opacity, _fcn_name) img_in_norm = img_in / 255.0 img_layer_norm = img_layer / 255.0 ratio = _compose_alpha(img_in_norm, img_layer_norm, opacity) comp = np.greater(img_layer_norm[:, :, :3], 0.5) \ * np.minimum(1.0 - ((1.0 - img_in_norm[:, :, :3]) * (1.0 - (img_layer_norm[:, :, :3] - 0.5) * 2.0)), 1.0) \ + np.logical_not(np.greater(img_layer_norm[:, :, :3], 0.5)) \ * np.minimum(img_in_norm[:, :, :3] * (img_layer_norm[:, :, :3] * 2.0), 1.0) ratio_rs = np.reshape(np.repeat(ratio, 3), [comp.shape[0], comp.shape[1], comp.shape[2]]) img_out = comp * ratio_rs + img_in_norm[:, :, :3] * (1.0 - ratio_rs) img_out = np.nan_to_num(np.dstack((img_out, img_in_norm[:, :, 3]))) # add alpha channel and replace nans return img_out * 255.0