-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwwmark.py
More file actions
327 lines (254 loc) · 11.6 KB
/
Copy pathwwmark.py
File metadata and controls
327 lines (254 loc) · 11.6 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import os
import sys
import cv2
import random
import ffmpeg
import tempfile
import numpy as np
from enum import Enum
from PIL import Image
from pdf2image import convert_from_path
from tqdm import tqdm
from colorama import Fore
BLIND = {
"seed": 20200209,
"alpha": 3.0
}
class OverlayText(Enum):
DEFAULT = BOTTOMRIGHT = {
"x": "w-tw-10",
"y": "h-th-10",
"alpha": "0.5",
"fontsize": "16",
"fontfile": "DroidSansFallbackFull.ttf",
}
class OverlayImage(Enum):
DEFAULT = TEST = {
"x": "(main_w-overlay_w)/2",
"y": "(main_h-overlay_h)/2",
"aa": "0.4",
}
CENTER = {
"x": "(main_w-overlay_w)/2",
"y": "(main_h-overlay_h)/2",
}
TOPLEFT = {
"x": "5",
"y": "5",
}
TOPRIGFHT = {
"x": "main_w-overlay_w-5",
"y": "5",
}
BOTTOMLEFT = {
"x": "5",
"y": "main_h-overlay_h",
}
BOTTOMRIGHT = {
"x": "main_w-overlay_w-5",
"y": "main_h-overlay_h-5",
}
def Binary2String(binary):
index = 0
string = []
rec = lambda x, i: x[2:8] + (rec(x[8:], i-1) if i > 1 else '') if x else ''
fun = lambda x, i: x[i+1:8] + rec(x[8:], i-1)
while index + 1 < len(binary):
chartype = binary[index:].index('0')
length = chartype*8 if chartype else 8
string.append(chr(int(fun(binary[index:index+length], chartype), 2)))
index += length
return ''.join(string)
# May be need a decorator to locate it
class Wwmark(object):
def __init__(self, i_file, i_mark, o_file, blind, **kwargs):
self.i_file = i_file if os.path.exists(i_file) else sys.exit(1)
self.i_mark = i_mark
self.o_file = o_file
self.kwargs = kwargs
self.blind = blind
self.action = None # "mark" or "clean"
def clean(self, type="image"):
if type == "image":
self.remove(type,out=self.o_file)
if type == "pdf":
self.action = "clean"
self.pdf()
print("[NOTICE]: Please checkout your output file {} {}".format(Fore.RED, self.o_file))
def remove(self,type="image",out=None):
if self.i_mark == None:
# Try without original mark file. Lucky ?
img = cv2.imread(self.i_file)
alpha = 2.0
beta = -160
new = np.clip(alpha * img + beta, 0, 255).astype(np.uint8)
cv2.imwrite(out, new)
else:
# For translucency watermark
# Code from https://stackoverflow.com/questions/32125281/removing-watermark-out-of-an-image-using-opencv/32141019
img = cv2.imread(self.i_file)
gr = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Make a copy of the grayscale image
bg = gr.copy()
# Apply morphological transformations
for i in tqdm(range(5),ascii=True, ncols=199, desc="[REMOVE] {} Processed ".format(self.i_file)):
kernel2 = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,
(2 * i + 1, 2 * i + 1))
bg = cv2.morphologyEx(bg, cv2.MORPH_CLOSE, kernel2)
bg = cv2.morphologyEx(bg, cv2.MORPH_OPEN, kernel2)
# Subtract the grayscale image from its processed copy
dif = cv2.subtract(bg, gr)
# Apply thresholding
bw = cv2.threshold(dif, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
dark = cv2.threshold(bg, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
# Extract pixels in the dark region
darkpix = gr[np.where(dark > 0)]
# Threshold the dark region to get the darker pixels inside it
darkpix = cv2.threshold(darkpix, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
# Paste the extracted darker pixels in the watermark region
bw[np.where(dark > 0)] = darkpix.T
cv2.imwrite(out, bw)
def show(self, mark="image"):
if mark == "image":
img = cv2.imread(self.i_file)
img_wm = cv2.imread(self.i_mark)
random.seed(BLIND.get("seed"))
m, n = list(
range(int(img.shape[0] * 0.5))), list(range(img.shape[1]))
random.shuffle(m)
random.shuffle(n)
f1 = np.fft.fft2(img)
f2 = np.fft.fft2(img_wm)
rwm = (f2 - f1) / BLIND.get("alpha")
rwm = np.real(rwm)
wm = np.zeros(rwm.shape)
for i in range(int(rwm.shape[0] * 0.5)):
for j in range(rwm.shape[1]):
wm[m[i]][n[j]] = np.uint8(rwm[i][j])
for i in tqdm(range(int(rwm.shape[0] * 0.5)), ncols=199, ascii=True, desc="[EXTRACT] {} try to find blind watermark".format(self.i_file),total=None):
for j in range(rwm.shape[1]):
wm[rwm.shape[0] - i - 1][rwm.shape[1] - j - 1] = wm[i][j]
assert cv2.imwrite(self.o_file, wm)
print("[NOTICE]: Please checkout your output file {} {}".format(Fore.RED, self.o_file))
else:
# I was wondering why it didn't work
if mark == "pdf":
with tempfile.TemporaryDirectory() as temp:
convert_from_path(self.i_file, output_folder=temp,
thread_count=1, output_file=lambda x: x+1)
self.i_file = os.path.join(temp, sorted(os.listdir(temp))[0])
img = Image.open(self.i_file).convert("RGBA")
# Decode text blind image
else:
img = Image.open(self.i_file)
pixels = list(img.getdata())
binary = ''.join([str(int(r >> 1 << 1 != r))+str(int(g >> 1 << 1 != g))+str(int(b >> 1 << 1 != b))+str(int(t >> 1 << 1 != t))
for (r, g, b, t) in tqdm(pixels, ncols=199, ascii=True, desc="[EXTRACT] {} try to find blind watermark".format(self.i_file))])
location = binary.find('0'*16)
endIndex = location+(8-(location%8)) if location%8!=0 else location
data = Binary2String(binary[0: endIndex])
print("[NOTICE]: {} {}".format(Fore.RED, data))
def pdf(self, mark="text"):
locs = None
if self.kwargs.get("location"):
flag = self.kwargs.pop("location")
if "," in flag:
locs = list(map(lambda x: int(x)-1,flag.split(",")))
elif "-" in flag:
loc = list(map(int,flag.split('-')))
locs = list(range(loc[0]-1 ,loc[1]))
else:
locs = [int(flag)]
ims = []
with tempfile.TemporaryDirectory() as temp:
# Original uuid generator is not sortable
convert_from_path(self.i_file, output_folder=temp,
thread_count=1, output_file=lambda x: x+1)
items = sorted(os.listdir(temp))
for k, item in tqdm(enumerate(items), ncols=199, ascii=True, desc="[{}] {} watermark with {}".format(str.upper(self.action) if self.action else "ADD", self.i_file, self.i_mark), unit="page"):
self.i_file = os.path.join(temp, item)
it = os.path.join(temp, "{}.png".format(item))
# this part code look like dirty
# if not found special locs, just rename it
if locs != None and locs.count(k) == 0:
os.rename(self.i_file,it)
else:
if self.action == "clean":
self.remove(out=it)
else:
if mark == "text":
self.text(path=it)
if mark == "image":
self.image(path=it)
with open(it, 'rb') as f:
im = Image.open(f)
im.load()
# ims.append(im)
ims.append(im.convert('RGB'))
ims[0].save(self.o_file, "PDF", resolution=100.0,
save_all=True, append_images=ims[1:])
print("[NOTICE]: Please checkout your output file {} {}".format(Fore.RED, self.o_file))
def text(self, path=None):
if not self.blind:
return self.save(ffmpeg.drawtext(ffmpeg.input(self.i_file), text=self.i_mark, **self.kwargs),path)
# blind text based on lsb
# This part code modified from internet
def zero_lsb(img):
pixels = list(img.getdata())
pixels_new = [(r>>1<<1, g>>1<<1, b>>1<<1, t>>1<<1) for (r, g, b, t) in pixels]
img_new = Image.new(img.mode, img.size)
img_new.putdata(pixels_new)
return img_new
def binary(integer):
return "0"*(8-(len(bin(integer))-2))+bin(integer).replace('0b','')
img = Image.open(self.i_file).convert("RGBA") # Convert it to 4 channel
img_zlsb = zero_lsb(img)
data_bin = ''.join(map(binary, bytearray(self.i_mark, 'utf-8')))
encodedPixels = [(r+int(data_bin[index*4+0]),
g+int(data_bin[index*4+1]),
b+int(data_bin[index*4+2]),
t+int(data_bin[index*4+3])) if index*4 < len(data_bin) else (r,g,b,t) for index, (r,g,b,t) in tqdm(enumerate(list(img_zlsb.getdata())), ncols=199, ascii=True, desc="[ADD] {} watermark with {}".format(self.i_file, self.o_file))]
encodedImage = Image.new(img.mode, img.size)
encodedImage.putdata(encodedPixels)
encodedImage.save(path if path else self.o_file)
print("[NOTICE]: Please checkout your output file {} {}".format(Fore.RED, self.o_file))
def image(self, path=None):
if not self.blind:
# colorchannelmixer would make it transparent
# Add "aa" as part of config, but still pass parameters with self.kwarges
return self.save(ffmpeg.overlay(ffmpeg.input(self.i_file), ffmpeg.input(self.i_mark).colorchannelmixer(aa=self.kwargs.pop("aa") if self.kwargs.get("aa") else "0.5" ), **self.kwargs),path)
# Blind Image
# This part code modified from https://github.com/chishaxie/BlindWaterMark
img = cv2.imread(self.i_file)
wm = cv2.imread(self.i_mark)
h, w = img.shape[0], img.shape[1]
hwm = np.zeros((int(h * 0.5), w, img.shape[2]))
assert hwm.shape[0] > wm.shape[0]
assert hwm.shape[1] > wm.shape[1]
hwm2 = np.copy(hwm)
for i in range(wm.shape[0]):
for j in range(wm.shape[1]):
hwm2[i][j] = wm[i][j]
random.seed(BLIND.get("seed"))
m, n = list(range(hwm.shape[0])), list(range(hwm.shape[1]))
random.shuffle(m)
random.shuffle(n)
for i in range(hwm.shape[0]):
for j in range(hwm.shape[1]):
hwm[i][j] = hwm2[m[i]][n[j]]
rwm = np.zeros(img.shape)
for i in range(hwm.shape[0]):
for j in range(hwm.shape[1]):
rwm[i][j] = hwm[i][j]
rwm[rwm.shape[0] - i - 1][rwm.shape[1] - j - 1] = hwm[i][j]
f1 = np.fft.fft2(img)
f2 = f1 + BLIND.get("alpha") * rwm
img_wm = np.real(np.fft.ifft2(f2))
assert cv2.imwrite(path if path else self.o_file, img_wm, [
int(cv2.IMWRITE_JPEG_QUALITY), 100])
print("[NOTICE]: Please checkout your output file {} {}".format(Fore.RED, self.o_file))
def save(self, stream, path=None):
if path is None:
path = self.o_file
ol = ffmpeg.output(stream, path)
ffmpeg.run(ol, overwrite_output=True, quiet=True)