-
Notifications
You must be signed in to change notification settings - Fork 0
/
Img_in_Img_Main.py
123 lines (80 loc) · 3.42 KB
/
Img_in_Img_Main.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
120
121
122
123
from PIL import Image
from Img_in_Img_Encode import *
from Img_in_Img_Decode import *
MAX_COLOR_VALUE = 256
MAX_BIT_VALUE = 8
''' def make_image(data, resolution):
image = Image.new("RGB", resolution)
image.putdata(data)
return image '''
''' def remove_n_least_significant_bits(value, n):
value = value >> n
return value << n '''
''' def get_n_least_significant_bits(value, n):
value = value << MAX_BIT_VALUE - n
value = value % MAX_COLOR_VALUE
return value >> MAX_BIT_VALUE - n '''
''' def get_n_most_significant_bits(value, n):
return value >> MAX_BIT_VALUE - n '''
''' def shit_n_bits_to_8(value, n):
return value << MAX_BIT_VALUE - n '''
'''
def encode(image_to_hide, image_to_hide_in, n_bits):
width, height = image_to_hide.size
hide_image = image_to_hide.load()
hide_in_image = image_to_hide_in.load()
data = []
for y in range(height):
for x in range(width):
(r_hide, g_hide, b_hide, alpha) = hide_image[x, y]
# (107, 3, 10)
# most sig bits
r_hide = get_n_most_significant_bits(r_hide, n_bits)
g_hide = get_n_most_significant_bits(g_hide, n_bits)
b_hide = get_n_most_significant_bits(b_hide, n_bits)
# remove lest n sig bits
(r_hide_in, g_hide_in, b_hide_in, alpha) = hide_in_image[x, y]
r_hide_in = remove_n_least_significant_bits(r_hide_in, n_bits)
g_hide_in = remove_n_least_significant_bits(g_hide_in, n_bits)
b_hide_in = remove_n_least_significant_bits(b_hide_in, n_bits)
data.append((r_hide + r_hide_in,
g_hide + g_hide_in,
b_hide + b_hide_in))
return make_image(data, image_to_hide.size) '''
''' def decode(image_to_decode, n_bits):
width, height = image_to_decode.size
encoded_image = image_to_decode.load()
data = []
for y in range(height):
for x in range(width):
r_encoded, g_encoded, b_encoded = encoded_image[x, y]
r_encoded = get_n_least_significant_bits(r_encoded, n_bits)
g_encoded = get_n_least_significant_bits(g_encoded, n_bits)
b_encoded = get_n_least_significant_bits(b_encoded, n_bits)
r_encoded = shit_n_bits_to_8(r_encoded, n_bits)
g_encoded = shit_n_bits_to_8(g_encoded, n_bits)
b_encoded = shit_n_bits_to_8(b_encoded, n_bits)
data.append((r_encoded, g_encoded, b_encoded))
return make_image(data, image_to_decode.size) '''
if "__main__":
encoded_image_path = "C:\Steganography\Encoded.png"
decoded_image_path = "C:\Steganography\Decoded.png"
n_bits = 2
print(":: Welcome to Steganography ::\n"
"1. Encode\n2. Decode\n")
a = int(input())
if (a == 1):
img = input(
"Enter the name of the image which you want to hide(with extension) : ")
image_to_hide = Image.open(img, 'r')
img1 = input(
"Enter the name of the image in which you want to hide(with extension) : ")
image_to_hide_in = Image.open(img1, 'r')
encode(image_to_hide, image_to_hide_in,
n_bits).save(encoded_image_path)
elif (a == 2):
img2 = input("Enter image name(with extension) : ")
image_to_decode = Image.open(img2, 'r')
decode(image_to_decode, n_bits).save(decoded_image_path)
else:
raise Exception("Enter correct input")