-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeembed.py
60 lines (53 loc) · 1.52 KB
/
deembed.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
import numpy as np
import cv2
import sys
from numpy import binary_repr
from operator import xor
def text_from_bits(bits):
chars = []
for b in range(len(bits) / 8):
byte = bits[b*8:(b+1)*8]
chars.append(chr(int(''.join([str(bit) for bit in byte]), 2)))
return ''.join(chars)
class DeEmbed():
def __init__(self, img):
self.image = img
self.size = img.size
self.shape = img.shape
self.rows = self.shape[0]
self.cols = self.shape[1]
def check4Delimiter(self, bits):
text = ""
delimiter = "0010111000100001"
if len(bits) < len(delimiter):
return False
for i in range(len(bits)):
text += str(bits[i])
if delimiter in text:
return True
else:
return False
def deEmbedSecret(self):
bits = []
for i in range(self.rows):
for j in range(self.cols):
if self.check4Delimiter(bits) == True:
break
pixel = self.image[i,j]
for k in range(0, 3):
if pixel[k]%2 == 0:
bits.append(0)
else:
bits.append(1)
result = text_from_bits(bits)
return result[:-2]
def main(av):
img = cv2.imread('new.png')
steg = DeEmbed(img)
secret = steg.deEmbedSecret()
print secret
cv2.waitKey(0)
cv2.destroyAllWindows()
if __name__=="__main__":
from sys import argv as av
main(av)