-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.py
119 lines (86 loc) · 2.83 KB
/
code.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
import cv2
import numpy as np
from PIL import Image
#it convert data in binary formate
def binaryformat(data):
if type(data) == str:
p = ''.join([format(ord(i), '08b')for i in data])
elif type(data) == bytes or type(data) == np.ndarray:
p = [format(i, '08b')for i in data]
return p
# hide data in given img
def hidedata(img, data):
data += "$$" #'$$'--> secrete key
data_index = 0
binary_data = binaryformat(data)
len_data = len(binary_data)
#iterate pixels from image and update pixel values
for value in img:
for pix in value:
r, g, b = binaryformat(pix)
if data_index < len_data:
pix[0] = int(r[:-1] + binary_data[data_index])
data_index += 1
if data_index < len_data:
pix[1] = int(g[:-1] + binary_data[data_index])
data_index += 1
if data_index < len_data:
pix[2] = int(b[:-1] + binary_data[data_index])
data_index += 1
if data_index >= len_data:
break
return img
def encode():
img_name = input("\nenter image name:")
image = cv2.imread(img_name)
img = Image.open(img_name, 'r')
w, h = img.size
data = input("\nenter message:")
if len(data) == 0:
raise ValueError("Empty data")
enc_img = input("\nenter encoded image name:")
enc_data = hidedata(image, data)
cv2.imwrite(enc_img, enc_data)
img1 = Image.open(enc_img, 'r')
img1 = img1.resize((w, h),Image.ANTIALIAS)
# optimize with 65% quality
if w != h:
img1.save(enc_img, optimize=True, quality=65)
else:
img1.save(enc_img)
# decoding
def find_data(img):
bin_data = ""
for value in img:
for pix in value:
r, g, b = binaryformat(pix)
bin_data += r[-1]
bin_data += g[-1]
bin_data += b[-1]
all_bytes = [bin_data[i: i + 8] for i in range(0, len(bin_data), 8)]
readable_data = ""
for x in all_bytes:
readable_data += chr(int(x, 2))
if readable_data[-2:] == "$$":
break
return readable_data[:-2]
def decode():
img_name = input("\nEnter Image name : ")
image = cv2.imread(img_name)
img=Image.open(img_name,'r')
msg = find_data(image)
return msg
def stegnography():
x = 1
while x != 0:
print('''\nImage stegnography
1.encode
2.decode''')
u_in = int(input("\n enter your choice:"))
if u_in == 1:
encode()
else:
ans = decode()
print("\n your message:"+ans)
x = int(input("\nenter 1 for continue otherwise 0:"))
stegnography()