-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlow_alpha_border_watermark.py
79 lines (73 loc) · 3.6 KB
/
low_alpha_border_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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import argparse
import os
from imutils import paths
from functions import *
parser = argparse.ArgumentParser()
parser.add_argument("-w", "--watermark", required=True,
help="path to watermark image (assumed to be transparent PNG)")
parser.add_argument("-i", "--input", required=True,
help="path to the input directory of images")
parser.add_argument("-o", "--output", required=True,
help="path to the output directory")
parser.add_argument("-wa", "--watermarkalpha", type=float, default=0.25,
help="alpha transparency of watermark between 0.0 and 1.0 (smaller is more transparent)")
parser.add_argument("-ba", "--borderalpha", type=float, default=0.5,
help="alpha transparency of border between 0.0 and 1.0 (smaller is more transparent)")
parser.add_argument("-g", "--greyness", type=float, default=1.0,
help="greyness of border between 0.0 and 1.0 (smaller is darker)")
parser.add_argument("-br", "--borderratio", type=float, default=0.05,
help="ratio of border to picture size")
parser.add_argument("-wr", "--watermarkratio", type=float, default=0.01,
help="ratio of space between watermark and border to picture size")
parser.add_argument("-s", "--size", type=float, default=0.2,
help="ratio of size of watermark to image size")
parser.add_argument("-c", "--compress", type=bool, default=False,
help="compress or not")
parser.add_argument("-k", "--key", type=str, default="kkkjgRvpZ75MJ470Fn9Cxfr5MG59ShdM",
help="tinify register key")
parser.add_argument("-d", "--darken", type=bool, default=False, help="darken background")
parser.add_argument("-da", "--darkamount", type=int, default=230, help="maximum pixel number")
arg = vars(parser.parse_args())
watermark_path = arg["watermark"]
input_path = arg["input"]
output_path = arg["output"]
watermark_alpha = arg["watermarkalpha"]
border_alpha = arg["borderalpha"]
greyness = arg["greyness"]
border_ratio = arg["borderratio"]
watermark_ratio = arg["watermarkratio"]
watermark_size = arg["size"]
is_compress = arg["compress"]
key = arg["key"]
dark = arg["darken"]
dark_num = arg["darkamount"]
watermark = cv2.imread(watermark_path, cv2.IMREAD_UNCHANGED)
(wH, wW) = watermark.shape[:2]
if is_compress:
set_tinify_key(key)
print(watermark.shape)
for imagePath in paths.list_images(input_path):
# load the input image, then add an extra dimension to the
# image (i.e., the alpha transparency)
image = cv2.imread(imagePath)
(h, w) = image.shape[:2]
posH = int(h * (border_ratio + watermark_ratio))
posW = int(w * (border_ratio + watermark_ratio))
watermark_h = int(watermark_size * h)
watermark_w = int(wW * watermark_h / wH)
watermark_resized = cv2.resize(watermark, (watermark_w, watermark_h), interpolation=cv2.INTER_LINEAR)
print("processing image: ", imagePath)
bordered_image = low_opacity_borderer(image, percent=border_ratio, alpha=border_alpha, greyness=greyness)
# 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
new_image = watermarker(watermark_resized, bordered_image, watermark_alpha, posH, posW)
if dark:
new_image = w_background_darker(new_image, dark_num)
filename = imagePath[imagePath.rfind(os.path.sep) + 1:]
p = os.path.sep.join((output_path, filename))
cv2.imwrite(p, new_image)
if is_compress:
image = compressor(p)
image.to_file(p)