Spaces:
Runtime error
Runtime error
Update create_print_layover.py
Browse files- create_print_layover.py +80 -80
create_print_layover.py
CHANGED
@@ -1,80 +1,80 @@
|
|
1 |
-
import numpy as np
|
2 |
-
|
3 |
-
|
4 |
-
def assert_image_format(image, fcn_name: str, arg_name: str, force_alpha: bool = True):
|
5 |
-
if not isinstance(image, np.ndarray):
|
6 |
-
err_msg = 'The blend_modes function "{fcn_name}" received a value of type "{var_type}" for its argument ' \
|
7 |
-
'"{arg_name}". The function however expects a value of type "np.ndarray" for this argument. Please ' \
|
8 |
-
'supply a variable of type np.ndarray to the "{arg_name}" argument.' \
|
9 |
-
.format(fcn_name=fcn_name, arg_name=arg_name, var_type=str(type(image).__name__))
|
10 |
-
raise TypeError(err_msg)
|
11 |
-
|
12 |
-
if not image.dtype.kind == 'f':
|
13 |
-
err_msg = 'The blend_modes function "{fcn_name}" received a numpy array of dtype (data type) kind ' \
|
14 |
-
'"{var_kind}" for its argument "{arg_name}". The function however expects a numpy array of the ' \
|
15 |
-
'data type kind "f" (floating-point) for this argument. Please supply a numpy array with the data ' \
|
16 |
-
'type kind "f" (floating-point) to the "{arg_name}" argument.' \
|
17 |
-
.format(fcn_name=fcn_name, arg_name=arg_name, var_kind=str(image.dtype.kind))
|
18 |
-
raise TypeError(err_msg)
|
19 |
-
|
20 |
-
if not len(image.shape) == 3:
|
21 |
-
err_msg = 'The blend_modes function "{fcn_name}" received a {n_dim}-dimensional numpy array for its argument ' \
|
22 |
-
'"{arg_name}". The function however expects a 3-dimensional array for this argument in the shape ' \
|
23 |
-
'(height x width x R/G/B/A layers). Please supply a 3-dimensional numpy array with that shape to ' \
|
24 |
-
'the "{arg_name}" argument.' \
|
25 |
-
.format(fcn_name=fcn_name, arg_name=arg_name, n_dim=str(len(image.shape)))
|
26 |
-
raise TypeError(err_msg)
|
27 |
-
|
28 |
-
if force_alpha and not image.shape[2] == 4:
|
29 |
-
err_msg = 'The blend_modes function "{fcn_name}" received a numpy array with {n_layers} layers for its ' \
|
30 |
-
'argument "{arg_name}". The function however expects a 4-layer array representing red, green, ' \
|
31 |
-
'blue, and alpha channel for this argument. Please supply a numpy array that includes all 4 layers ' \
|
32 |
-
'to the "{arg_name}" argument.' \
|
33 |
-
.format(fcn_name=fcn_name, arg_name=arg_name, n_layers=str(image.shape[2]))
|
34 |
-
raise TypeError(err_msg)
|
35 |
-
|
36 |
-
|
37 |
-
def assert_opacity(opacity, fcn_name: str, arg_name: str = 'opacity'):
|
38 |
-
if not isinstance(opacity, float) and not isinstance(opacity, int):
|
39 |
-
err_msg = 'The blend_modes function "{fcn_name}" received a variable of type "{var_type}" for its argument ' \
|
40 |
-
'"{arg_name}". The function however expects the value passed to "{arg_name}" to be of type ' \
|
41 |
-
'"float". Please pass a variable of type "float" to the "{arg_name}" argument of function ' \
|
42 |
-
'"{fcn_name}".' \
|
43 |
-
.format(fcn_name=fcn_name, arg_name=arg_name, var_type=str(type(opacity).__name__))
|
44 |
-
raise TypeError(err_msg)
|
45 |
-
|
46 |
-
if not 0.0 <= opacity <= 1.0:
|
47 |
-
err_msg = 'The blend_modes function "{fcn_name}" received the value "{val}" for its argument "{arg_name}". ' \
|
48 |
-
'The function however expects that the value for "{arg_name}" is inside the range 0.0 <= x <= 1.0. ' \
|
49 |
-
'Please pass a variable in that range to the "{arg_name}" argument of function "{fcn_name}".' \
|
50 |
-
.format(fcn_name=fcn_name, arg_name=arg_name, val=str(opacity))
|
51 |
-
raise ValueError(err_msg)
|
52 |
-
|
53 |
-
|
54 |
-
def _compose_alpha(img_in, img_layer, opacity):
|
55 |
-
comp_alpha = np.minimum(img_in[:, :, 3], img_layer[:, :, 3]) * opacity
|
56 |
-
new_alpha = img_in[:, :, 3] + (1.0 - img_in[:, :, 3]) * comp_alpha
|
57 |
-
np.seterr(divide='ignore', invalid='ignore')
|
58 |
-
ratio = comp_alpha / new_alpha
|
59 |
-
ratio[ratio == np.
|
60 |
-
return ratio
|
61 |
-
|
62 |
-
|
63 |
-
def create_hard_light_layover(img_in, img_layer, opacity, disable_type_checks: bool = False):
|
64 |
-
if not disable_type_checks:
|
65 |
-
_fcn_name = 'hard_light'
|
66 |
-
assert_image_format(img_in, _fcn_name, 'img_in')
|
67 |
-
assert_image_format(img_layer, _fcn_name, 'img_layer')
|
68 |
-
assert_opacity(opacity, _fcn_name)
|
69 |
-
img_in_norm = img_in / 255.0
|
70 |
-
img_layer_norm = img_layer / 255.0
|
71 |
-
ratio = _compose_alpha(img_in_norm, img_layer_norm, opacity)
|
72 |
-
comp = np.greater(img_layer_norm[:, :, :3], 0.5) \
|
73 |
-
* np.minimum(1.0 - ((1.0 - img_in_norm[:, :, :3])
|
74 |
-
* (1.0 - (img_layer_norm[:, :, :3] - 0.5) * 2.0)), 1.0) \
|
75 |
-
+ np.logical_not(np.greater(img_layer_norm[:, :, :3], 0.5)) \
|
76 |
-
* np.minimum(img_in_norm[:, :, :3] * (img_layer_norm[:, :, :3] * 2.0), 1.0)
|
77 |
-
ratio_rs = np.reshape(np.repeat(ratio, 3), [comp.shape[0], comp.shape[1], comp.shape[2]])
|
78 |
-
img_out = comp * ratio_rs + img_in_norm[:, :, :3] * (1.0 - ratio_rs)
|
79 |
-
img_out = np.nan_to_num(np.dstack((img_out, img_in_norm[:, :, 3]))) # add alpha channel and replace nans
|
80 |
-
return img_out * 255.0
|
|
|
1 |
+
import numpy as np
|
2 |
+
|
3 |
+
|
4 |
+
def assert_image_format(image, fcn_name: str, arg_name: str, force_alpha: bool = True):
|
5 |
+
if not isinstance(image, np.ndarray):
|
6 |
+
err_msg = 'The blend_modes function "{fcn_name}" received a value of type "{var_type}" for its argument ' \
|
7 |
+
'"{arg_name}". The function however expects a value of type "np.ndarray" for this argument. Please ' \
|
8 |
+
'supply a variable of type np.ndarray to the "{arg_name}" argument.' \
|
9 |
+
.format(fcn_name=fcn_name, arg_name=arg_name, var_type=str(type(image).__name__))
|
10 |
+
raise TypeError(err_msg)
|
11 |
+
|
12 |
+
if not image.dtype.kind == 'f':
|
13 |
+
err_msg = 'The blend_modes function "{fcn_name}" received a numpy array of dtype (data type) kind ' \
|
14 |
+
'"{var_kind}" for its argument "{arg_name}". The function however expects a numpy array of the ' \
|
15 |
+
'data type kind "f" (floating-point) for this argument. Please supply a numpy array with the data ' \
|
16 |
+
'type kind "f" (floating-point) to the "{arg_name}" argument.' \
|
17 |
+
.format(fcn_name=fcn_name, arg_name=arg_name, var_kind=str(image.dtype.kind))
|
18 |
+
raise TypeError(err_msg)
|
19 |
+
|
20 |
+
if not len(image.shape) == 3:
|
21 |
+
err_msg = 'The blend_modes function "{fcn_name}" received a {n_dim}-dimensional numpy array for its argument ' \
|
22 |
+
'"{arg_name}". The function however expects a 3-dimensional array for this argument in the shape ' \
|
23 |
+
'(height x width x R/G/B/A layers). Please supply a 3-dimensional numpy array with that shape to ' \
|
24 |
+
'the "{arg_name}" argument.' \
|
25 |
+
.format(fcn_name=fcn_name, arg_name=arg_name, n_dim=str(len(image.shape)))
|
26 |
+
raise TypeError(err_msg)
|
27 |
+
|
28 |
+
if force_alpha and not image.shape[2] == 4:
|
29 |
+
err_msg = 'The blend_modes function "{fcn_name}" received a numpy array with {n_layers} layers for its ' \
|
30 |
+
'argument "{arg_name}". The function however expects a 4-layer array representing red, green, ' \
|
31 |
+
'blue, and alpha channel for this argument. Please supply a numpy array that includes all 4 layers ' \
|
32 |
+
'to the "{arg_name}" argument.' \
|
33 |
+
.format(fcn_name=fcn_name, arg_name=arg_name, n_layers=str(image.shape[2]))
|
34 |
+
raise TypeError(err_msg)
|
35 |
+
|
36 |
+
|
37 |
+
def assert_opacity(opacity, fcn_name: str, arg_name: str = 'opacity'):
|
38 |
+
if not isinstance(opacity, float) and not isinstance(opacity, int):
|
39 |
+
err_msg = 'The blend_modes function "{fcn_name}" received a variable of type "{var_type}" for its argument ' \
|
40 |
+
'"{arg_name}". The function however expects the value passed to "{arg_name}" to be of type ' \
|
41 |
+
'"float". Please pass a variable of type "float" to the "{arg_name}" argument of function ' \
|
42 |
+
'"{fcn_name}".' \
|
43 |
+
.format(fcn_name=fcn_name, arg_name=arg_name, var_type=str(type(opacity).__name__))
|
44 |
+
raise TypeError(err_msg)
|
45 |
+
|
46 |
+
if not 0.0 <= opacity <= 1.0:
|
47 |
+
err_msg = 'The blend_modes function "{fcn_name}" received the value "{val}" for its argument "{arg_name}". ' \
|
48 |
+
'The function however expects that the value for "{arg_name}" is inside the range 0.0 <= x <= 1.0. ' \
|
49 |
+
'Please pass a variable in that range to the "{arg_name}" argument of function "{fcn_name}".' \
|
50 |
+
.format(fcn_name=fcn_name, arg_name=arg_name, val=str(opacity))
|
51 |
+
raise ValueError(err_msg)
|
52 |
+
|
53 |
+
|
54 |
+
def _compose_alpha(img_in, img_layer, opacity):
|
55 |
+
comp_alpha = np.minimum(img_in[:, :, 3], img_layer[:, :, 3]) * opacity
|
56 |
+
new_alpha = img_in[:, :, 3] + (1.0 - img_in[:, :, 3]) * comp_alpha
|
57 |
+
np.seterr(divide='ignore', invalid='ignore')
|
58 |
+
ratio = comp_alpha / new_alpha
|
59 |
+
ratio[ratio == np.nan] = 0.0
|
60 |
+
return ratio
|
61 |
+
|
62 |
+
|
63 |
+
def create_hard_light_layover(img_in, img_layer, opacity, disable_type_checks: bool = False):
|
64 |
+
if not disable_type_checks:
|
65 |
+
_fcn_name = 'hard_light'
|
66 |
+
assert_image_format(img_in, _fcn_name, 'img_in')
|
67 |
+
assert_image_format(img_layer, _fcn_name, 'img_layer')
|
68 |
+
assert_opacity(opacity, _fcn_name)
|
69 |
+
img_in_norm = img_in / 255.0
|
70 |
+
img_layer_norm = img_layer / 255.0
|
71 |
+
ratio = _compose_alpha(img_in_norm, img_layer_norm, opacity)
|
72 |
+
comp = np.greater(img_layer_norm[:, :, :3], 0.5) \
|
73 |
+
* np.minimum(1.0 - ((1.0 - img_in_norm[:, :, :3])
|
74 |
+
* (1.0 - (img_layer_norm[:, :, :3] - 0.5) * 2.0)), 1.0) \
|
75 |
+
+ np.logical_not(np.greater(img_layer_norm[:, :, :3], 0.5)) \
|
76 |
+
* np.minimum(img_in_norm[:, :, :3] * (img_layer_norm[:, :, :3] * 2.0), 1.0)
|
77 |
+
ratio_rs = np.reshape(np.repeat(ratio, 3), [comp.shape[0], comp.shape[1], comp.shape[2]])
|
78 |
+
img_out = comp * ratio_rs + img_in_norm[:, :, :3] * (1.0 - ratio_rs)
|
79 |
+
img_out = np.nan_to_num(np.dstack((img_out, img_in_norm[:, :, 3]))) # add alpha channel and replace nans
|
80 |
+
return img_out * 255.0
|