-
Notifications
You must be signed in to change notification settings - Fork 0
/
stego.py
181 lines (148 loc) · 4.54 KB
/
stego.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
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
import cv2
def load_image(name):
image = cv2.imread(filename=name)
cv2.imshow("Img", image)
# cv2.waitKey(0)
return image
def manipulate_pixels_rand(img):
w, h, _ = img.shape
print("Size of Image: ", w, h, ". Possible characters = ", w * h * 3 / 8, "Words with average length of 6: ",
w * h * 3 / 8 / 6)
for x in range(w):
for y in range(h):
(b, g, r) = img[x, y]
img[x, y] = (255, 0, r)
return img
def hide_bin_text_in_img(img, text):
w, h, _ = img.shape
print("Size of Image: ", w, h, ". Possible characters = ", w * h * 3 / 8, "Words with average length of 6: ",
w * h * 3 / 8 / 6)
i = 0
break_loop = 0
for x in range(w):
for y in range(h):
(b, g, r) = img[x, y]
b_bin = list(format(b, 'b'))
g_bin = list(format(g, 'b'))
r_bin = list(format(r, 'b'))
try:
b_bin[len(b_bin) - 1] = text[i]
except:
break_loop = 1
if break_loop != 1:
try:
g_bin[len(g_bin) - 1] = text[i + 1]
except:
break_loop = 1
if break_loop != 1:
try:
r_bin[len(r_bin) - 1] = text[i + 2]
except:
break_loop = 1
b = bin_to_dec(b_bin)
g = bin_to_dec(g_bin)
r = bin_to_dec(r_bin)
img[x, y] = (b, g, r)
i = i + 3
if break_loop == 1:
break
return img
def text_to_bits(text):
bin_text = ''.join(format(ord(i), '08b') for i in text)
return bin_text
def bin_to_dec(bin_text_slice):
decimal = 0
for i in range(len(bin_text_slice)):
if bin_text_slice[i] != '0':
# 8-1 = 7 highest bit index - index -> when beginning
# at the highest bit
decimal = decimal + pow(2, len(bin_text_slice) - i - 1)
return decimal
def bits_to_text(bin_text):
str_data = ' '
for i in range(0, len(bin_text), 8):
temp_data = bin_text[i:i + 8]
decimal_data = bin_to_dec(temp_data)
str_data = str_data + chr(decimal_data)
return str_data
def extract_text_from_image(img, len):
w, h, _ = img.shape
i = 0
break_loop = 0
len = len * 8
text_bin = ''
for x in range(w):
for y in range(h):
(b, g, r) = img[x, y]
b_bin = test_channel_and_fix_length_to_list(b)
g_bin = test_channel_and_fix_length_to_list(g)
r_bin = test_channel_and_fix_length_to_list(r)
if i >= len:
break_loop = 1
break
text_bin = text_bin + b_bin[7]
i = i + 1
if i >= len:
break_loop = 1
break
text_bin = text_bin + g_bin[7]
i = i + 1
if i >= len:
break_loop = 1
break
text_bin = text_bin + r_bin[7]
i = i + 1
if i >= len:
break_loop = 1
break
if break_loop:
break
print(text_bin)
return bits_to_text(text_bin)
def load_text(path):
with open(path) as f:
text = f.read()
f.close()
return text
def test_channel_and_fix_length_to_list(c):
c_bin = list(format(c, 'b'))
if len(c_bin) < 8:
append = '0' * (8 - len(c_bin))
c_bin = list(append + ''.join(c_bin))
return c_bin
def get_image_with_user_path():
print("Image path:")
path = input()
if path == '':
path = 'doge.jpg'
print("Image path =", path)
return load_image(path)
def get_text_with_user_path():
print("(1) Textfile or (2) input or (any) default text?")
option = input()
text = ''
if option == '1':
print('Textfile path:')
text_path = input()
print('Textfile path =', text_path)
text = load_text(text_path)
elif option == '2':
print('Text:')
text = input()
print('Text =', text)
else:
print('Default lorem ipsum. ~ 15000w, 100000chars')
text = load_text('lorem_ipsum.txt')
return text
def manipulate_img():
img = get_image_with_user_path()
text = get_text_with_user_path()
bin_text = text_to_bits(text)
bits_to_text(bin_text)
img = hide_bin_text_in_img(img, bin_text)
cv2.imshow("New", img)
cv2.imwrite('stega.jpg', img)
cv2.waitKey(0)
print(extract_text_from_image(img, len(text)))
if __name__ == "__main__":
manipulate_img()