-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhide_image_in_image.py
363 lines (284 loc) · 14.6 KB
/
hide_image_in_image.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
"""
This application hides an image inside another image.
It can be done using either 1 or 2 color pixels.
The application can also decode the image and retrive the hidden image.
NOTICE! when encoding you must use .jpg formats.
Example use of application (encode):
An example of the use, where we want to encode a the private.jpg image in the public.jpg.
We use encode2, to tell the application we want to use 2 color pixels.
Command: python hideInImage.py encode2 public.jpg private.jpg
Output: secret.png
Example use of application (decode):
Here we want to decode the encoded image named secret.png.
We use decode2 beacus we used to color pixels of the image.
It will then output the hidden image inside the secret.png.
Command: python hideInImage.py decode2 secret.png
Output: output.png
TODO:
- Make it possible to take .png files too
Author: Mathias Nicolajsen (TidosDK)
"""
# Imported libraries
import sys
from PIL import Image
def encode_1(public_image, private_image): # Encoding function for 1 color pixel - Takes two arguments
"""
This function encodes the private_image inside the public_image.
It uses 1 color pixel.
"""
public_img = Image.open(public_image) # Gets information for the public image
private_img = Image.open(private_image) # Gets information for the private image
x_public_max = public_img.size[0] # The max for the public image on the x-coordinate
x_public = 0
y_public = 0
# Gets the size information for the private image:
width = '{0:016b}'.format(private_img.size[0])
height = '{0:016b}'.format(private_img.size[1])
for i in range(1, 17): # Saves the width for the private image
if x_public == x_public_max:
y_public += 1
x_public = 0
size_x = '{0:08b}'.format(public_img.getpixel((x_public, y_public))[0])[:7] + '{0}'.format(int(width[i-1]))
other_colors = public_img.getpixel((x_public, y_public))
public_img.putpixel((x_public, y_public), (int(size_x, 2), other_colors[1], other_colors[2]))
x_public += 1
for i in range(1, 17): # Save the height for the private image
if x_public == x_public_max:
y_public += 1
x_public = 0
size_y = '{0:08b}'.format(public_img.getpixel((x_public, y_public))[0])[:7] + '{0}'.format(int(height[i-1]))
other_colors = public_img.getpixel((x_public, y_public))
public_img.putpixel((x_public, y_public), (int(size_y, 2), other_colors[1], other_colors[2]))
x_public += 1
for x in range(private_img.size[0]):
for y in range(private_img.size[1]):
red_private = '{0:08b}'.format(private_img.getpixel((x, y))[0]) # Converts red color to binary
for i in range(1, 9): # Red
if x_public == x_public_max:
y_public += 1
x_public = 0
red = '{0:08b}'.format(public_img.getpixel((x_public, y_public))[0])[:7] + '{0}'.format(int(red_private[i-1]))
other_colors = public_img.getpixel((x_public, y_public))
public_img.putpixel((x_public, y_public), (int(red, 2), other_colors[1], other_colors[2]))
x_public += 1
green_private = '{0:08b}'.format(private_img.getpixel((x, y))[1]) # Converts green color to binary
for i in range(1, 9): # Green
if x_public == x_public_max:
y_public += 1
x_public = 0
green = '{0:08b}'.format(public_img.getpixel((x_public, y_public))[1])[:7] + '{0}'.format(int(green_private[i-1]))
other_colors = public_img.getpixel((x_public, y_public))
public_img.putpixel((x_public, y_public), (other_colors[0], int(green, 2), other_colors[2]))
x_public += 1
blue_private = '{0:08b}'.format(private_img.getpixel((x, y))[2]) # Converts blue color to binary
for i in range(1, 9): # Blue
if x_public == x_public_max:
y_public += 1
x_public = 0
blue = '{0:08b}'.format(public_img.getpixel((x_public, y_public))[2])[:7] + '{0}'.format(int(blue_private[i-1]))
other_colors = public_img.getpixel((x_public, y_public))
public_img.putpixel((x_public, y_public), (other_colors[0], other_colors[1], int(blue, 2)))
x_public += 1
return public_img # Returns the public image with the private image inside.
def decode_1(encoded_image): # Decoding function for 1 color pixel - Takes one argument
"""
This function decodes the image inside another image.
It only reads 1 color pixel.
"""
public_img = Image.open(encoded_image) # Gets information for public image
x_public_max = public_img.size[0]
x_public = 0
y_public = 0
x_private_size = ""
y_private_size = ""
for _ in range(1, 17):
if x_public == x_public_max:
y_public += 1
x_public = 0
x_private_size += '{0:08b}'.format(public_img.getpixel((x_public, y_public))[0])[-1]
x_public += 1
for _ in range(1, 17):
if x_public == x_public_max:
y_public += 1
x_public = 0
y_private_size += '{0:08b}'.format(public_img.getpixel((x_public, y_public))[0])[-1]
x_public += 1
private_img = Image.new("RGB", (int(x_private_size, 2), int(y_private_size, 2)))
for x in range(int(x_private_size, 2)): # Loops through the x-coordinate for public image.
for y in range(int(y_private_size, 2)): # Loops through the y-coordinate for public image.
red = ""
green = ""
blue = ""
for _ in range(1, 9): # Red
if x_public == x_public_max:
y_public += 1
x_public = 0
red += '{0:08b}'.format(public_img.getpixel((x_public, y_public))[0])[-1]
x_public += 1
for _ in range(1, 9): # Green
if x_public == x_public_max:
y_public += 1
x_public = 0
green += '{0:08b}'.format(public_img.getpixel((x_public, y_public))[1])[-1]
x_public += 1
for _ in range(1, 9): # Blue
if x_public == x_public_max:
y_public += 1
x_public = 0
blue += '{0:08b}'.format(public_img.getpixel((x_public, y_public))[2])[-1]
x_public += 1
private_img.putpixel((x, y), (int(red, 2), int(green, 2), int(blue, 2)))
return private_img
# Encode function with 2 color pixels
def encode_2(public_image, private_image): # Function for encoding image
"""
This function encodes the private_image inside the public_image.
It uses 2 color pixel.
"""
public_img = Image.open(public_image) # Gets information for public image
private_img = Image.open(private_image) # Gets information for private image
x_public_max = public_img.size[0]
x_public = 0
y_public = 0
x_size = '{0:016b}'.format(private_img.size[0])
y_size = '{0:016b}'.format(private_img.size[1])
for l in range(1, 16, 2):
if x_public == x_public_max:
y_public += 1
x_public = 0
size_x = '{0:08b}'.format(public_img.getpixel((x_public, y_public))[0])[:6] + '{0}'.format(str(x_size[l-1:l+1]))
other_colors = public_img.getpixel((x_public, y_public))
public_img.putpixel((x_public, y_public), (int(size_x, 2), other_colors[1], other_colors[2]))
x_public += 1
for l in range(1, 16, 2): # Image size y
if x_public == x_public_max:
y_public += 1
x_public = 0
size_y = '{0:08b}'.format(public_img.getpixel((x_public, y_public))[0])[:6] + '{0}'.format(str(y_size[l-1:l+1]))
other_colors = public_img.getpixel((x_public, y_public))
public_img.putpixel((x_public, y_public), (int(size_y, 2), other_colors[1], other_colors[2]))
x_public += 1
for x in range(private_img.size[0]):
for y in range(private_img.size[1]):
red_private = '{0:08b}'.format(private_img.getpixel((x, y))[0]) # Converts red color to binary
for l in range(1, 8, 2): # Red
if x_public == x_public_max:
y_public += 1
x_public = 0
red = '{0:08b}'.format(public_img.getpixel((x_public, y_public))[0])[:6] + '{0}'.format(str(red_private[l-1:l+1]))
other_colors = public_img.getpixel((x_public, y_public))
public_img.putpixel((x_public, y_public), (int(red, 2), other_colors[1], other_colors[2]))
x_public += 1
green_private = '{0:08b}'.format(private_img.getpixel((x, y))[1]) # Converts green color to binary
for l in range(1, 8, 2): # Green
if x_public == x_public_max:
y_public += 1
x_public = 0
green = '{0:08b}'.format(public_img.getpixel((x_public, y_public))[1])[:6] + '{0}'.format(str(green_private[l-1:l+1]))
other_colors = public_img.getpixel((x_public, y_public))
public_img.putpixel((x_public, y_public), (other_colors[0], int(green, 2), other_colors[2]))
x_public += 1
blue_private = '{0:08b}'.format(private_img.getpixel((x, y))[2]) # Converts blue color to binary
for l in range(1, 8, 2): # Blue
if x_public == x_public_max:
y_public += 1
x_public = 0
blue = '{0:08b}'.format(public_img.getpixel((x_public, y_public))[2])[:6] + '{0}'.format(str(blue_private[l-1:l+1]))
other_colors = public_img.getpixel((x_public, y_public))
public_img.putpixel((x_public, y_public), (other_colors[0], other_colors[1], int(blue, 2)))
x_public += 1
return public_img # Returns the public image with the private image inside.
def decode_2(public_image): # Function for decoding image
"""
This function decodes the image inside another image.
It only reads 2 color pixel.
"""
public_img = Image.open(public_image) # Gets information for public image
x_public_max = public_img.size[0]
x_public = 0
y_public = 0
x_private_size = ""
y_private_size = ""
for _ in range(1, 16, 2):
if x_public == x_public_max:
y_public += 1
x_public = 0
x_private_size += '{0:08b}'.format(public_img.getpixel((x_public, y_public))[0])[-2:8]
x_public += 1
for _ in range(1, 16, 2):
if x_public == x_public_max:
y_public += 1
x_public = 0
y_private_size += '{0:08b}'.format(public_img.getpixel((x_public, y_public))[0])[-2:8]
x_public += 1
print(int(x_private_size, 2), int(y_private_size, 2))
private_img = Image.new("RGB", (int(x_private_size, 2), int(y_private_size, 2)))
for x in range(int(x_private_size, 2)): # Loops through the x-coordinate for public image.
for y in range(int(y_private_size, 2)): # Loops through the y-coordinate for public image.
red = ""
green = ""
blue = ""
for _ in range(1, 5): # Red
if x_public == x_public_max:
y_public += 1
x_public = 0
red += '{0:08b}'.format(public_img.getpixel((x_public, y_public))[0])[-2:8]
x_public += 1
for _ in range(1, 5): # Green
if x_public == x_public_max:
y_public += 1
x_public = 0
green += '{0:08b}'.format(public_img.getpixel((x_public, y_public))[1])[-2:8]
x_public += 1
for _ in range(1, 5): # Blue
if x_public == x_public_max:
y_public += 1
x_public = 0
blue += '{0:08b}'.format(public_img.getpixel((x_public, y_public))[2])[-2:8]
x_public += 1
private_img.putpixel((x, y), (int(red, 2), int(green, 2), int(blue, 2)))
return private_img
if len(sys.argv) >= 4:
if sys.argv[1] == "encode1":
temp_public_img = Image.open(sys.argv[2])
temp_private_img = Image.open(sys.argv[3])
temp_public_size = temp_public_img.size[0] * temp_public_img.size[1]
temp_private_img = temp_private_img.size[0] * temp_private_img.size[1]
if temp_public_size > temp_private_img * 24:
print("Private image can fit inside the public.")
print("Begins to encode the private image in the public...\n")
encode_1(sys.argv[2], sys.argv[3]).save("secret.png")
print("Image has successfully been saved as secret.png")
else:
print("ERROR: Private image is too bit to fit")
elif sys.argv[1] == "encode2":
temp_public_img = Image.open(sys.argv[2])
temp_private_img = Image.open(sys.argv[3])
temp_public_size = temp_public_img.size[0] * temp_public_img.size[1]
temp_private_img = temp_private_img.size[0] * temp_private_img.size[1]
if temp_public_size > temp_private_img * 12:
print("Private image can fit inside the public.")
print("Begins to encode the private image in the public...\n")
encode_2(sys.argv[2], sys.argv[3]).save("secret.png")
print("Image has successfully been saved as secret.png")
else:
print("ERROR: Private image is too bit to fi,")
elif len(sys.argv) >= 3:
if sys.argv[1] == "decode1":
print("Make sure you used the right color pixel argument")
print("Application will now decode the image...\n")
decode_1(sys.argv[2]).save("output.png")
print("Image has been saved as output.png")
elif sys.argv[1] == "decode2":
print("Make sure you used the right color pixel argument")
print("Application will now decode the image...\n")
decode_2(sys.argv[2]).save("output.png")
print("Image has been saved as output.png")
else:
print("The right uses:")
print("1 color pixel:")
print(" - Python file.py encode1 img1 img2")
print(" - Python file.py decode1 img")
print("\n2 color pixels:")
print(" - Python file.py encode2 img1 img2")
print(" - Python file.py decode2 img")