-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathadd_watermark.py
59 lines (30 loc) · 1.17 KB
/
add_watermark.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
import cv2
img = cv2.imread('diego-jimenez-A-NVHPka9Rk-unsplash.jpg')
watermark = cv2.imread("Watermark.jpg")
percent_of_scaling = 20
new_width = int(img.shape[1] * percent_of_scaling / 100)
new_height = int(img.shape[0] * percent_of_scaling / 100)
new_dim = (new_width, new_height)
resized_img = cv2.resize(img, new_dim, interpolation=cv2.INTER_AREA)
wm_scale = 40
wm_width = int(watermark.shape[1] * wm_scale / 100)
wm_height = int(watermark.shape[0] * wm_scale / 100)
wm_dim = (wm_width, wm_height)
resized_wm = cv2.resize(watermark, wm_dim, interpolation=cv2.INTER_AREA)
h_img, w_img, _ = resized_img.shape
center_y = int(h_img / 2)
center_x = int(w_img / 2)
h_wm, w_wm, _ = resized_wm.shape
top_y = center_y - int(h_wm / 2)
left_x = center_x - int(w_wm / 2)
bottom_y = top_y + h_wm
right_x = left_x + w_wm
roi = resized_img[top_y:bottom_y, left_x:right_x]
result = cv2.addWeighted(roi, 1, resized_wm, 0.3, 0)
resized_img[top_y:bottom_y, left_x:right_x] = result
filename = 'Watermakred_Image.jpg'
cv2.imwrite(filename, resized_img)
cv2.imshow("Resized Input Image", resized_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
# click on show files to view the output image