-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimgSteganography.py
143 lines (119 loc) · 4.48 KB
/
imgSteganography.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
# image_path = "C:\\Users\\Joshua Ong\\PycharmProjects\\CSC2005-Coursework-1\\imageTest.png"
import cv2
import numpy as np
from PIL import Image
from PIL import GifImagePlugin
import os
def to_bin(data):
if isinstance(data, str):
return ''.join([format(ord(i), "08b") for i in data])
elif isinstance(data, bytes) or isinstance(data, np.ndarray):
return [format(i, "08b") for i in data]
elif isinstance(data, int) or isinstance(data, np.uint8):
return format(data, "08b")
else:
raise TypeError("Type is not supported")
def encode_image(image_path, secretData, number_of_bits):
image = cv2.imread(image_path)
max_bytes = image.shape[0] * image.shape[1] * 3 // 8
print("Maximum bytes to encode: ", max_bytes)
secretData += "#####"
if len(secretData) > max_bytes:
raise ValueError("Insufficient bits")
print("Encoding image")
dataIndex = 0
binSecretData = to_bin(secretData)
dataLen = len(binSecretData)
counter = 128
count = 0
encode_text = open('encode.txt', 'w')
for row in image:
for pixel in row:
r, g, b = to_bin(pixel)
info = ""
for i in range(number_of_bits):
if dataIndex < dataLen:
info += binSecretData[dataIndex]
dataIndex += 1
else:
info += "0"
pixel[0] = int(r[:-number_of_bits] + info, 2)
info = ""
for i in range(number_of_bits):
if dataIndex < dataLen:
info += binSecretData[dataIndex]
dataIndex += 1
else:
info += "0"
pixel[1] = int(g[:-number_of_bits] + info, 2)
info = ""
for i in range(number_of_bits):
if dataIndex < dataLen:
info += binSecretData[dataIndex]
dataIndex += 1
else:
info += "0"
pixel[2] = int(b[:-number_of_bits] + info, 2)
if count<counter:
encode_text.write(str(pixel))
encode_text.write('\n')
count+=1
if dataIndex >= dataLen:
break
encode_text.close()
file_name, file_extension = os.path.splitext(image_path)
#cv2.imwrite(file_name + "_encoded" + file_extension, image, [cv2.IMWRITE_JPEG_QUALITY, 100])
encoded_image_name = "encoded_image.png"
if image_path.endswith(".bmp"):
encoded_image_name = "encoded_image.bmp"
cv2.imwrite(encoded_image_name, image, [cv2.IMWRITE_JPEG_QUALITY, 100])
print("Image successfully encoded: "+ file_name + "_encoded" + file_extension)
#return file_name + "_encoded" + file_extension
return encoded_image_name
def decode_image(image_path, number_of_bits):
print("Decoding image")
image = cv2.imread(image_path)
binData = ""
counter = 128
count = 0
decode_text = open('decode.txt', 'w')
for row in image:
for pixel in row:
if count < counter:
decode_text.write(str(pixel))
decode_text.write('\n')
count += 1
r, g, b = to_bin(pixel)
binData += r[-number_of_bits:]
binData += g[-number_of_bits:]
binData += b[-number_of_bits:]
decode_text.close()
allBytes = [binData[i: i + 8] for i in range(0, len(binData), 8)]
decodedData = ""
for byte in allBytes:
decodedData += chr(int(byte, 2))
if decodedData[-5:] == "#####":
break
print("Image successfully decoded")
return decodedData[:-5]
# Example usage
# image_encode_path = "C:\\Users\\Joshua Ong\\PycharmProjects\\CSC2005-Coursework-1\\imageTest.png"
# image_decode_path = "C:\\Users\\Joshua Ong\\PycharmProjects\\CSC2005-Coursework-1\\imageTest_encoded.png"
# data_to_encode = "test secret"
# number_of_bits = 1
# Encode the data into the image
# encode_image(image_encode_path, data_to_encode, number_of_bits)
# Decode the data from the encoded image
# decoded_data = decode_image(image_decode_path, number_of_bits)
# print("Decoded data:", decoded_data)
# imageObject = Image.open("./testgif.gif")
# print("number of frames:", imageObject.n_frames)
# for frameIndex in range(0, imageObject.n_frames):
# imageObject.seek(0)
# frame = imageObject.copy()
# rgb_data = list(imageObject.convert('RGB').getdata())
# # r, g, b = rgb_data
# binary_data = []
# for rgb in rgb_data:
# r,g,b = rgb
# print(r)