-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathembed.py
69 lines (61 loc) · 2.14 KB
/
embed.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
import numpy as np
import cv2
import sys
from numpy import binary_repr
from operator import xor
def text_to_bits(s):
result = []
for c in s:
bits = bin(ord(c))[2:]
bits = '00000000'[len(bits):] + bits
result.extend([int(b) for b in bits])
return result
class Embed():
def __init__(self, img):
self.image = img
self.shape = img.shape
self.rows = self.shape[0]
self.cols = self.shape[1]
def checkTextSize(self, text):
delimiter = "0010111000100001"
if 8 * len(text) + len(delimiter) >= self.rows * self.cols * 3:
print "Text size too big to embed"
def embedSecret(self, text):
bits = []
bits = text_to_bits(text)
delimiter = [0,0,1,0,1,1,1,0,0,0,1,0,0,0,0,1] #text= ".!"
b = 0
d = 0
for i in range(self.rows):
for j in range(self.cols):
pixel = self.image[i,j]
for k in range(0, 3):
if len(bits) + len(delimiter) == b + d:
break
if b == len(bits) and d < len(delimiter):
if pixel[k]%2 != 0 and delimiter[d] == 0:
self.image[i,j][k] = self.image[i,j][k] - 1
if pixel[k]%2 == 0:
self.image[i,j][k] = xor(self.image[i,j][k], delimiter[d])
d += 1
else:
if pixel[k]%2 != 0 and bits[b] == 0:
self.image[i,j][k] = self.image[i,j][k] - 1
if pixel[k]%2 == 0:
self.image[i,j][k] = xor(self.image[i,j][k], bits[b])
b += 1
return self.image
def main(av):
img = cv2.imread('image.png')
steg = Embed(img)
user_input = raw_input('Enter the text: ')
#user_input = 'hello' * 1000
steg.checkTextSize(user_input)
result = steg.embedSecret(user_input)
cv2.imwrite("new.png", result)
cv2.imshow("new.png",result)
cv2.waitKey()
cv2.destroyAllWindows()
if __name__=="__main__":
from sys import argv as av
main(av)