File size: 5,024 Bytes
0831268
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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