-
Notifications
You must be signed in to change notification settings - Fork 1
/
stego.py
113 lines (84 loc) · 2.46 KB
/
stego.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
# source: https://github.com/djrobin17/image-stego-tool
# import libraries
import sys
import numpy as np
from PIL import Image
np.set_printoptions(threshold=sys.maxsize)
# encoding function
def Encode(src, message, dest):
img = Image.open(src, 'r')
width, height = img.size
array = np.array(list(img.getdata()))
if img.mode == 'RGB':
n = 3
m = 0
elif img.mode == 'RGBA':
n = 4
m = 1
total_pixels = array.size//n
message += "$t3g0"
b_message = ''.join([format(ord(i), "08b") for i in message])
req_pixels = len(b_message)
if req_pixels > total_pixels:
print("ERROR: Need larger file size")
else:
index = 0
for p in range(total_pixels):
for q in range(m, n):
if index < req_pixels:
array[p][q] = int(
bin(array[p][q])[2:9] + b_message[index], 2)
index += 1
array = array.reshape(height, width, n)
enc_img = Image.fromarray(array.astype('uint8'), img.mode)
enc_img.save(dest)
print("Image Encoded Successfully")
# decoding function
def Decode(src):
img = Image.open(src, 'r')
array = np.array(list(img.getdata()))
if img.mode == 'RGB':
n = 3
m = 0
elif img.mode == 'RGBA':
n = 4
m = 1
total_pixels = array.size//n
hidden_bits = ""
for p in range(total_pixels):
for q in range(m, n):
hidden_bits += (bin(array[p][q])[2:][-1])
hidden_bits = [hidden_bits[i:i+8] for i in range(0, len(hidden_bits), 8)]
message = ""
for i in range(len(hidden_bits)):
if message[-5:] == "$t3g0":
break
else:
message += chr(int(hidden_bits[i], 2))
if "$t3g0" in message:
return message[:-5]
else:
return "No Hidden Message Found"
# main function
def Stego():
print("--Welcome to $t3g0--")
print("1: Encode")
print("2: Decode")
func = input()
if func == '1':
print("Enter Source Image Path")
src = input()
print("Enter Message to Hide")
message = input()
print("Enter Destination Image Path")
dest = input()
print("Encoding...")
Encode(src, message, dest)
elif func == '2':
print("Enter Source Image Path")
src = input()
print("Decoding...")
Decode(src)
else:
print("ERROR: Invalid option chosen")
# Stego()