-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.py
97 lines (83 loc) · 3.55 KB
/
functions.py
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import cv2
import numpy as np
import tinify
def watermarker(watermark, image, alpha, posH, posW):
(wH, wW) = watermark.shape[:2]
(B, G, R, A) = cv2.split(watermark)
B = cv2.bitwise_and(B, B, mask=A)
G = cv2.bitwise_and(G, G, mask=A)
R = cv2.bitwise_and(R, R, mask=A)
watermark = cv2.merge([B, G, R, A])
(h, w) = image.shape[:2]
watermark_new = watermark.copy()
w_bg_mean = image[posH: wH + posH, posW: wW + posW].mean()
if w_bg_mean < 100:
watermark_new[0: wH, 0:wW, 0:3] = 255 - watermark_new[0: wH, 0:wW, 0: 3]
print("changed")
image = np.dstack([image, np.ones((h, w), dtype="uint8") * 255])
# construct an overlay that is the same size as the input
# image, (using an extra dimension for the alpha transparency),
# then add the watermark to the overlay in the bottom-right
# corner
overlay = np.zeros((h, w, 4), dtype="uint8")
print(posH)
overlay[posH: wH + posH, posW: wW + posW] = watermark_new
new_image = np.zeros((h, w, 4), dtype="uint8")
row_number = 0
while row_number < wH + posH:
column_number = 0
while column_number < wW + posW:
if (overlay[row_number][column_number][3]) < 0.5:
new_image[row_number][column_number] = image[row_number][column_number]
else:
cv2.addWeighted(image[row_number][column_number], 1 - alpha,
overlay[row_number][column_number],
alpha, 0, new_image[row_number][column_number])
column_number += 1
row_number += 1
new_image[row_number:h, 0: w] = image[row_number:h, 0: w]
new_image[0: row_number, column_number: w] = image[0: row_number, column_number: w]
return new_image
def low_opacity_borderer(image, percent, alpha, greyness=1.0):
greyness_255 = greyness * 255
greyness_255 = np.dtype(np.uint8).type(greyness_255)
border_color = [greyness_255, greyness_255, greyness_255]
alpha_255 = alpha * 255
alpha_255 = np.dtype(np.uint8).type(alpha_255)
(h, w) = image.shape[:2]
start_height = int(h * percent)
end_height = h - start_height
start_width = int(w * percent)
end_width = w - start_width
black_image = np.zeros((h, w, 3), dtype=np.uint8)
black_image[:start_height + 1, :] = border_color
black_image[:, :start_width + 1, :] = border_color
black_image[end_height:, :] = border_color
black_image[:, end_width:] = border_color
image_new = cv2.addWeighted(black_image, alpha, image, 1, 0)
return image_new
def blurer(image, percent, kernel_size=(5, 5)):
(h, w) = image.shape[:2]
start_height = int(h * percent)
end_height = h - start_height
start_width = int(w * percent)
end_width = w - start_width
black_image = np.zeros((h, w, 3), dtype=np.uint8)
black_image[start_height: end_height + 1, start_width: end_width + 1] = \
image[start_height: end_height + 1, start_width: end_width + 1]
new_image = cv2.blur(image, kernel_size)
black_image[:start_height + 1, :] = new_image[:start_height + 1, :]
black_image[:, :start_width + 1, :] = new_image[:, :start_width + 1, :]
black_image[end_height:, :] = new_image[end_height:, :]
black_image[:, end_width:] = new_image[:, end_width:]
return black_image
def set_tinify_key(key):
tinify.key = key
def compressor(input_path):
source = tinify.from_file(input_path)
return source
def w_background_darker(image, max_needed):
max_pix = np.max(image)
diff = max_pix - max_needed
image[image > max_needed] -= diff
return image