Skip to content

Commit 508d67c

Browse files
authored
Image steganography
1 parent d99f6d6 commit 508d67c

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed

Image Steganography.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import cv2
2+
import numpy as np
3+
import pyfiglet
4+
5+
banner = pyfiglet.figlet_format("STEGBYTE")
6+
print(banner,"\t\t\t\tCreated By Soumalya")
7+
8+
9+
def data2binary(data):
10+
if type(data) == str:
11+
return ''.join([format(ord(i),"08b") for i in data])
12+
elif type(data) == bytes or type(data) == np.ndarray:
13+
return [format(i,"08b") for i in data]
14+
15+
16+
def hideData(image,secret_data):
17+
secret_data += "#####"
18+
19+
data_index = 0
20+
binary_data = data2binary(secret_data)
21+
data_length = len(binary_data)
22+
23+
for values in image:
24+
for pixel in values:
25+
26+
r,g,b = data2binary(pixel)
27+
28+
if data_index < data_length:
29+
pixel[0] = int(r[:-1] + binary_data[data_index])
30+
data_index += 1
31+
if data_index < data_length:
32+
pixel[1] = int(g[:-1] + binary_data[data_index])
33+
data_index += 1
34+
if data_index < data_length:
35+
pixel[2] = int(b[:-1] + binary_data[data_index])
36+
data_index += 1
37+
if data_index >= data_length:
38+
break
39+
40+
return image
41+
42+
43+
def encode_text():
44+
image_name = input("Enter Cover Image Name : ")
45+
image = cv2.imread(image_name)
46+
47+
data = input("Enter The Text You Want To Hide : ")
48+
if data == 0:
49+
raise ValueError("Data is Empty ... ")
50+
51+
file_name = input("Enter The Encoded Image Name : ")
52+
53+
encoded_data = hideData(image,data)
54+
cv2.imwrite(file_name,encoded_data)
55+
56+
def show_data(image):
57+
binary_data = ""
58+
for values in image:
59+
for pixel in values:
60+
r,g,b = data2binary(pixel)
61+
62+
binary_data += r[-1]
63+
binary_data += g[-1]
64+
binary_data += b[-1]
65+
66+
all_bytes = [binary_data[i: i+8] for i in range (0,len(binary_data),8)]
67+
68+
decoded_data = ""
69+
for byte in all_bytes:
70+
decoded_data += chr(int(byte,2))
71+
if decoded_data[-5:] == "#####":
72+
break
73+
74+
return decoded_data[:-5]
75+
76+
77+
78+
def decode_text():
79+
image_name = input("Enter Image You Want To Extract : ")
80+
image = cv2.imread(image_name)
81+
82+
text=show_data(image)
83+
return text
84+
85+
def stegnography():
86+
userinput = int(input("\nSTEGBYTE\n\n 1. Encode \n 2. Decode \n\n Enter Here : "))
87+
if userinput == 1:
88+
encode_text()
89+
else:
90+
final_data=decode_text()
91+
print("\nDecoded Data : ",final_data)
92+
93+
stegnography()

0 commit comments

Comments
 (0)