-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
323 lines (273 loc) · 10.6 KB
/
app.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
from PIL import Image
import numpy as np
import datetime
from werkzeug.utils import secure_filename
import sys
import codecs
count = 0
#Binary to UTF
def bin_to_utf(data):
Unicode_data = ''
for d in data:
binary_int = int(d,2)
byte_number = binary_int.bit_length() + 7 # 8
binary_array = binary_int.to_bytes(byte_number, "big")
ascii_text = binary_array.decode("utf-8", 'ignore')
Unicode_data = Unicode_data + ascii_text
return Unicode_data
# Decode the image
def decode(image):
arr = np.array(image)
red = arr[..., 0] # All Red values
green = arr[..., 1] # All Green values
blue = arr[..., 2] # All Blue values
height,width = blue.shape
total_size = height*width
data = []
bit_size = 0
data_byte = ''
if count < total_size:
new_count = 0
for i in range(height):
for j in range(width):
if new_count <= count:
if bit_size < 8:
data_byte = data_byte + str((blue[i][j] & 1))
bit_size+=1
else:
data.append(data_byte)
bit_size = 0
data_byte = ''
data_byte = data_byte + str((blue[i][j] & 1))
bit_size+=1
new_count+=1
else:
break
elif count > total_size and count < 2*total_size:
new_count = 0
for i in range(height):
for j in range(width):
if bit_size < 8:
data_byte = data_byte + str((blue[i][j] & 1))
bit_size+=1
else:
data.append(data_byte)
bit_size = 0
data_byte = ''
data_byte = data_byte + str((blue[i][j] & 1))
bit_size+=1
bit_size = 0
data_byte = ''
for i in range(height):
for j in range(width):
if new_count <= count:
if bit_size < 8:
data_byte = data_byte + str((green[i][j] & 1))
bit_size+=1
else:
data.append(data_byte)
bit_size = 0
data_byte = ''
data_byte = data_byte + str((green[i][j] & 1))
bit_size+=1
new_count+=1
else:
break
else:
new_count = 0
for i in range(height):
for j in range(width):
if bit_size < 8:
data_byte = data_byte + str((blue[i][j] & 1))
bit_size+=1
else:
data.append(data_byte)
bit_size = 0
data_byte = ''
data_byte = data_byte + str((blue[i][j] & 1))
bit_size+=1
bit_size = 0
data_byte = ''
for i in range(height):
for j in range(width):
if bit_size < 8:
data_byte = data_byte + str((green[i][j] & 1))
bit_size+=1
else:
data.append(data_byte)
bit_size = 0
data_byte = ''
data_byte = data_byte + str((green[i][j] & 1))
bit_size+=1
bit_size = 0
data_byte = ''
for i in range(height):
for j in range(width):
if new_count <= count:
if bit_size < 8:
data_byte = data_byte + str((red[i][j] & 1))
bit_size+=1
else:
data.append(data_byte)
bit_size = 0
data_byte = ''
data_byte = data_byte + str((red[i][j] & 1))
bit_size+=1
new_count+=1
else:
break
return data
# Encode the image
def encode(image,data):
# iterate the image pixels
# store user input in LSB(least significant bit) of each pixels RGB depending on need
# start with B then G then R..
arr = np.array(image)
# print(arr.shape) ...to see size of img
# 3d array....2d pixel array with 3 channels
red = arr[..., 0] # All Red values
green = arr[..., 1] # All Green values
blue = arr[..., 2] # All Blue values
# pixels can be odd or even
height,width = blue.shape
incompBlue = True
incompGreen = True
incompRed = True
blue[0][0] = 193
blue[0][2] = 882
i = 0
j = -1
global count
c = 0
for char in data:
for bit in char:
count += 1
if incompBlue == True:
if i < height:
if j < width:
j+=1
if j >= width:
i+=1
j=0
# print("{0:b}".format(blue[i][j]))
# if bit of char is 1
#print(i,j)
if i < height:
if bit=='1':
blue[i][j] = blue[i][j] | 1 #set the last bit to 1
elif bit=='0':
blue[i][j] = blue[i][j] & (blue[i][j] -1) #set the last bit to 0
c += 1
else:
incompBlue = False
i = 0
j = -1
# print("{0:b}".format(blue[i][j]))
# print('\n')
else:
incompBlue = False
i = 0
j = -1
if incompBlue == False and incompGreen == True:
if i < height:
if j < width:
j+=1
if j >= width:
i+=1
j=0
# print("{0:b}".format(green[i][j]))
# if bit of char is 1
# print("g",i,j)
if i < height:
if bit=='1':
green[i][j] = green[i][j] | 1 #set the last bit to 1
elif bit=='0':
green[i][j] = green[i][j] & (green[i][j] -1) #set the last bit to 0
c += 1
else:
incompGreen = False
i = 0
j = -1
# print("{0:b}".format(green[i][j]))
# print('\n')
else:
incompGreen = False
i = 0
j = -1
if incompBlue == False and incompGreen == False:
if i < height:
if j < width:
j+=1
if j >= width:
i+=1
j=0
# print("{0:b}".format(red[i][j]))
# if bit of char is 1
# print("r",i,j)
if i < height:
if bit=='1':
red[i][j] = red[i][j] | 1 #set the last bit to 1
elif bit=='0':
red[i][j] = red[i][j] & (red[i][j] -1) #set the last bit to 0
c += 1
else:
incompRed = False
sys.exit("Choose a higher quality image")
# print("{0:b}".format(red[i][j]))
# print('\n')
else:
incompRed = False
i = 0
j = -1
break
if incompRed == False:
sys.exit("Choose a higher quality image")
w, h = image.size
test = np.zeros((h, w, 3), dtype=np.uint8)
#reconstructing image
test[:,:,0] = red
test[:,:,1] = green
test[:,:,2] = blue
# current time and filename
ct = datetime.datetime.now()
filename = secure_filename('Amaterasu '+str(ct) +'.jpg')
# saving image
img = Image.fromarray(test, 'RGB')
img.save('./output/'+ filename)
# img.show()
return [img,filename]
# Convert user input to 8 bit binary using Unicode value of characters
def generate_binary(data):
# converted data
new_data = []
for i in data:
# converting every character of user input to its binary
new_data.append(format(ord(i), '08b'))
# ord returns the unicode of string(of unit length only..so a character basically)
# character to Unicode to binary
# unicode preferred over Ascii as superset of ascii and characters of other languages also available
return new_data
# Load image
def load_img():
resource_name = "./resources/mangekyo.jpg"
image = Image.open(resource_name,'r')
return image
# Main function
def main():
with open('./resources/input.txt', 'r') as file:
content = file.read()
#content = input("Enter the content you want to hide:\n")
data = generate_binary(content)
image = load_img()
encoded_img,fname = encode(image,data)
decoded_data = decode(encoded_img)
result_data = bin_to_utf(decoded_data)
tmp = result_data.replace('\0','')
print("Encoded image is present in output folder with name",fname,"\n")
print("Decoded message from image is present in output folder with name decode.txt")
file = codecs.open('./output/decoded.txt', 'w')
file.write(tmp)
file.close()
# Driver code
if __name__ == '__main__':
main()