-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_w_img.py
100 lines (66 loc) · 2.96 KB
/
check_w_img.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
98
99
100
from PIL import Image
MAX_COLOR_VALUE = 256
MAX_BIT_VALUE = 8
def make_image(data, resolution):
image = Image.new("RGB", resolution)
image.putdata(data)
return image
def remove_n_least_significant_bits(value,n):
value = value >> n
return value << n
def get_n_least_significant_bits(value, n):
value = value << MAX_BIT_VALUE - n
value = value % MAX_COLOR_VALUE
return value >> MAX_BIT_VALUE - n
def get_n_most_significant_bits(value, n):
return value >> MAX_BIT_VALUE - n
def shit_n_bits_to_8(value, n):
return value << MAX_BIT_VALUE - n
def encode(image_to_hide, image_to_hide_in, n_bits):
width, height = image_to_hide.size
hide_image = image_to_hide.load()
hide_in_image = image_to_hide_in.load()
data = []
for y in range(height):
for x in range(width):
# (107, 3, 10)
# most sig bits
r_hide, g_hide, b_hide = hide_image[x,y]
r_hide = get_n_most_significant_bits(r_hide, n_bits)
g_hide = get_n_most_significant_bits(g_hide, n_bits)
b_hide = get_n_most_significant_bits(b_hide, n_bits)
# remove lest n sig bits
r_hide_in, g_hide_in, b_hide_in = hide_in_image[x,y]
r_hide_in = remove_n_least_significant_bits(r_hide_in, n_bits)
g_hide_in = remove_n_least_significant_bits(g_hide_in, n_bits)
b_hide_in = remove_n_least_significant_bits(b_hide_in, n_bits)
data.append((r_hide + r_hide_in,
g_hide + g_hide_in,
b_hide + b_hide_in))
return make_image(data, image_to_hide.size)
def decode(image_to_decode, n_bits):
width, height = image_to_decode.size
encoded_image = image_to_decode.load()
data = []
for y in range(height):
for x in range(width):
r_encoded, g_encoded, b_encoded = encoded_image[x, y]
r_encoded = get_n_least_significant_bits(r_encoded, n_bits)
g_encoded = get_n_least_significant_bits(g_encoded, n_bits)
b_encoded = get_n_least_significant_bits(b_encoded, n_bits)
r_encoded = shit_n_bits_to_8(r_encoded, n_bits)
g_encoded = shit_n_bits_to_8(g_encoded, n_bits)
b_encoded = shit_n_bits_to_8(b_encoded, n_bits)
data.append((r_encoded, g_encoded, b_encoded))
return make_image(data, image_to_decode.size)
if "__main__":
image_to_hide_path = "./kiwi.jpg"
image_to_hide_in_path = "data/frame43.jpg"
encoded_image_path = "./encoded.jpg"
decoded_image_path = "./decoded.jpg"
n_bits = 2
image_to_hide = Image.open(image_to_hide_path)
image_to_hide_in = Image.open(image_to_hide_in_path)
# encode(image_to_hide, image_to_hide_in, n_bits).save(encoded_image_path)
image_to_decode = Image.open(encoded_image_path)
decode(image_to_decode, n_bits).save(decoded_image_path)