-
Notifications
You must be signed in to change notification settings - Fork 87
/
Steganography.py
183 lines (144 loc) · 5.03 KB
/
Steganography.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
182
183
#Steganography
import cv2
import time
from os import stat
HEADER_SIZE = 30
FILE_NAME = 20
FILE_SIZE = 10
def splitByte(val):
#104 ---> [011, 010, 00]
bits =[]
bits.append((val & 0xE0)>>5)
bits.append((val & 0x1C)>>2)
bits.append((val & 0x3))
return bits
def mergeBits(bits):
#[011, 010, 00] ---> 104
return (((bits[0] << 3) | bits[1]) << 2) | bits[2]
def getsize(document):
try:
# using os.stat to find the size of the file in bytes
return stat(document).st_size
except:
return -1
def processDocumentName(document):
# document: 'd:/rahulcomp/images/kids.jpg'
fname = document.split('/')[-1]
if len(fname) > FILE_NAME:
# slice the fname
return fname[len(fname)-FILE_NAME:]
else:
# left pad with _
return fname.rjust(FILE_NAME, '_')
def processDocumentSize(document):
size = getsize(document)
if size == -1:
return None
s_size = str(size)
if len(s_size) > FILE_SIZE:
return None
return s_size.rjust(FILE_SIZE, '_')
def embed(imageFile, document, resultantFile):
start_time = time.time()
# read the source image
arr_img = cv2.imread(imageFile, cv2.IMREAD_COLOR)
# if imageFile exists then src_img is a 3D numpy array (h,w,(b,g,r))
# otherswise src_img is None
if arr_img is None:
print('Source Image Not Found')
return
w = arr_img.shape[1]
h = arr_img.shape[0]
capacity = w*h
print('Embedding Capacity : ', capacity)
doc_size = getsize(document)
if doc_size == -1:
print(document, 'not found')
return
print('Required Capacity : ', doc_size)
if doc_size + HEADER_SIZE > capacity:
print('Cannot embed', document , 'in', imageFile)
return
header = processDocumentName(document) + processDocumentSize(document)
#open the document
fh = open(document, 'rb')
#Embedding
i=cnt= 0
flag = True
while i < h and flag:
j=0
while j < w:
if cnt < HEADER_SIZE:
#data comes from the header
data = ord(header[cnt])
else:
#data comes from the file in byte object form
#and it is None at EOF
data = fh.read(1)
if data:
#fetched, now convert the byte object to int
data = int.from_bytes(data, byteorder='big')
else:
#EOF : stop embedding
fh.close()
flag = False
break
#data available now
bits = splitByte(data)
#LSB overwriting
arr_img[i,j,0] = (arr_img[i,j,0] & ~0x7) | bits[0] #b
arr_img[i,j,1] = (arr_img[i,j,1] & ~0x7) | bits[1] #g
arr_img[i,j,2] = (arr_img[i,j,2] & ~0x3) | bits[2] #r
cnt+=1
j+=1
i+=1
#save back the image
cv2.imwrite(resultantFile, arr_img)
print('embedding done in ', time.time()- start_time, 'seconds')
def extract(imageFile, resultantFolder):
start_time = time.time()
# read the source image
arr_img = cv2.imread(imageFile, cv2.IMREAD_COLOR)
# if imageFile exists then src_img is a 3D numpy array (h,w,(b,g,r))
# otherswise src_img is None
if arr_img is None:
print('Source Image Not Found')
return
w = arr_img.shape[1]
h = arr_img.shape[0]
#Extraction
i=cnt= 0
flag = True
header = ''
while i < h and flag:
j=0
while j < w:
#extract bits from the pixel
b1 = arr_img[i,j,0] & 0x7
b2 = arr_img[i,j,1] & 0x7
b3 = arr_img[i,j,2] & 0x3
#form the byte by merging the bits
data = mergeBits([b1,b2,b3])
if cnt < HEADER_SIZE:
#recreate the header
header = header + chr(data)
else:
if cnt == HEADER_SIZE:
#extract content and recreate the document
doc_name = resultantFolder + '/' + header[:FILE_NAME].strip('_')
doc_size = int(header[FILE_NAME:].strip('_')) + cnt
fh = open(doc_name, 'wb')
if cnt < doc_size:
#binary writing requires byte objects and not the ASCII, hence convert.
fh.write(int.to_bytes(int(data), 1, byteorder='big'))
else:
fh.close()
print('Extraction Complete in ', time.time()- start_time, 'seconds')
print('See: ' , doc_name)
flag = False
break
cnt+=1
j+=1
i+=1
embed('d:/rahulcomp/images/work.jpg', 'd:/rahulcomp/images/kids.jpg', 'd:/rahulcomp/images/result.png')
extract('d:/rahulcomp/images/result.png', 'd:/rahulcomp/temp')