forked from kamil-nawrot/digital-watermarking
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
135 lines (88 loc) · 3.67 KB
/
Copy pathtest.py
File metadata and controls
135 lines (88 loc) · 3.67 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
import numpy as np
import pywt
import os
from PIL import Image
from scipy.fftpack import dct
from scipy.fftpack import idct
current_path = str(os.path.dirname(__file__))
image = 'images/lena.jpg'
watermark = 'images/mandrill_256.jpg'
def convert_image(image_name, size):
img = Image.open(image_name).resize((size, size), 1)
img = img.convert('L')
img.save(image_name)
image_array = np.array(img.getdata(), dtype=np.float).reshape((size, size))
print(image_array[0][0])
print(image_array[10][10])
return image_array
def process_coefficients(imArray, model, level):
coeffs=pywt.wavedec2(data = imArray, wavelet = model, level = level)
print(coeffs)
# print coeffs[0].__len__()
coeffs_H=list(coeffs)
return coeffs_H
def embed_watermark(watermark_array, orig_image):
watermark_array_size = len(watermark_array[0])
watermark_flat = watermark_array.ravel()
ind = 0
for x in range (0, orig_image.__len__(), 8):
for y in range (0, orig_image.__len__(), 8):
if ind < watermark_flat.__len__():
subdct = orig_image[x:x+8, y:y+8]
subdct[5][5] = watermark_flat[ind]
orig_image[x:x+8, y:y+8] = subdct
ind += 1
return orig_image
def apply_dct(image_array):
size = image_array[0].__len__()
all_subdct = np.empty((size, size))
for i in range (0, size, 8):
for j in range (0, size, 8):
subpixels = image_array[i:i+8, j:j+8]
subdct = dct(dct(subpixels.T, norm="ortho").T, norm="ortho")
all_subdct[i:i+8, j:j+8] = subdct
return all_subdct
def inverse_dct(all_subdct):
size = all_subdct[0].__len__()
all_subidct = np.empty((size, size))
for i in range (0, size, 8):
for j in range (0, size, 8):
subidct = idct(idct(all_subdct[i:i+8, j:j+8].T, norm="ortho").T, norm="ortho")
all_subidct[i:i+8, j:j+8] = subidct
return all_subidct
def get_watermark(dct_watermarked_coeff, watermark_size):
subwatermarks = []
for x in range (0, dct_watermarked_coeff.__len__(), 8):
for y in range (0, dct_watermarked_coeff.__len__(), 8):
coeff_slice = dct_watermarked_coeff[x:x+8, y:y+8]
subwatermarks.append(coeff_slice[5][5])
watermark = np.array(subwatermarks).reshape(watermark_size, watermark_size)
return watermark
def recover_watermark(image_array, model='haar', level = 1):
coeffs_watermarked_image = process_coefficients(image_array, model, level=level)
dct_watermarked_coeff = apply_dct(coeffs_watermarked_image[0])
watermark_array = get_watermark(dct_watermarked_coeff, 128)
watermark_array = np.uint8(watermark_array)
#Save result
img = Image.fromarray(watermark_array)
img.save('recovered_watermark.jpg')
def print_image_from_array(image_array, name):
image_array_copy = image_array.clip(0, 255)
image_array_copy = image_array_copy.astype("uint8")
img = Image.fromarray(image_array_copy)
img.save(name)
def w2d(img):
model = 'haar'
level = 1
image_array = convert_image(image, 2048)
watermark_array = convert_image(watermark, 128)
coeffs_image = process_coefficients(image_array, model, level=level)
dct_array = apply_dct(coeffs_image[0])
dct_array = embed_watermark(watermark_array, dct_array)
coeffs_image[0] = inverse_dct(dct_array)
# reconstruction
image_array_H=pywt.waverec2(coeffs_image, model)
print_image_from_array(image_array_H, 'image_with_watermark.jpg')
# recover images
recover_watermark(image_array = image_array_H, model=model, level = level)
w2d("images/lena.jpg")