Create utils.py
Browse files
utils.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
def to_bin(data):
|
| 2 |
+
"""Convert `data` to binary format as string"""
|
| 3 |
+
if isinstance(data, str):
|
| 4 |
+
return ''.join([ format(ord(i), "08b") for i in data ])
|
| 5 |
+
elif isinstance(data, bytes):
|
| 6 |
+
return ''.join([ format(i, "08b") for i in data ])
|
| 7 |
+
elif isinstance(data, np.ndarray):
|
| 8 |
+
return [ format(i, "08b") for i in data ]
|
| 9 |
+
elif isinstance(data, int) or isinstance(data, np.uint8):
|
| 10 |
+
return format(data, "08b")
|
| 11 |
+
else:
|
| 12 |
+
raise TypeError("Type not supported.")
|
| 13 |
+
def decode(image_name,txt=None):
|
| 14 |
+
msg=""
|
| 15 |
+
BGRimage = cv2.imread(image_name)
|
| 16 |
+
image = cv2.cvtColor(BGRimage, cv2.COLOR_BGR2RGB)
|
| 17 |
+
binary_data = ""
|
| 18 |
+
for row in image:
|
| 19 |
+
for pixel in row:
|
| 20 |
+
r, g, b = to_bin(pixel)
|
| 21 |
+
binary_data += r[-1]
|
| 22 |
+
binary_data += g[-1]
|
| 23 |
+
binary_data += b[-1]
|
| 24 |
+
all_bytes = [ binary_data[i: i+8] for i in range(0, len(binary_data), 8) ]
|
| 25 |
+
decoded_data = ""
|
| 26 |
+
for byte in all_bytes:
|
| 27 |
+
decoded_data += chr(int(byte, 2))
|
| 28 |
+
if decoded_data[-5:] == "=====":
|
| 29 |
+
break
|
| 30 |
+
this = decoded_data[:-5].split("#####",1)[0]
|
| 31 |
+
this = eval(this)
|
| 32 |
+
enc_in=this
|
| 33 |
+
return this,msg
|
| 34 |
+
|
| 35 |
+
def encode(image_name, secret_data,txt=None):
|
| 36 |
+
msg=""
|
| 37 |
+
BGRimage = cv2.imread(image_name)
|
| 38 |
+
image = cv2.cvtColor(BGRimage, cv2.COLOR_BGR2RGB)
|
| 39 |
+
n_bytes = image.shape[0] * image.shape[1] * 3 // 8
|
| 40 |
+
print("[*] Maximum bytes to encode:", n_bytes)
|
| 41 |
+
secret_data1=secret_data
|
| 42 |
+
while True:
|
| 43 |
+
if len(secret_data1)+5 < (n_bytes):
|
| 44 |
+
secret_data1 = f'{secret_data1}#####'
|
| 45 |
+
elif len(secret_data1)+5 >= (n_bytes):
|
| 46 |
+
break
|
| 47 |
+
secret_data = secret_data1
|
| 48 |
+
if len(secret_data) > n_bytes:
|
| 49 |
+
return image_name,msg
|
| 50 |
+
secret_data += "====="
|
| 51 |
+
data_index = 0
|
| 52 |
+
binary_secret_data = to_bin(secret_data)
|
| 53 |
+
data_len = len(binary_secret_data)
|
| 54 |
+
for row in image:
|
| 55 |
+
for pixel in row:
|
| 56 |
+
r, g, b = to_bin(pixel)
|
| 57 |
+
if data_index < data_len:
|
| 58 |
+
pixel[0] = int(r[:-1] + binary_secret_data[data_index], 2)
|
| 59 |
+
data_index += 1
|
| 60 |
+
if data_index < data_len:
|
| 61 |
+
pixel[1] = int(g[:-1] + binary_secret_data[data_index], 2)
|
| 62 |
+
data_index += 1
|
| 63 |
+
if data_index < data_len:
|
| 64 |
+
pixel[2] = int(b[:-1] + binary_secret_data[data_index], 2)
|
| 65 |
+
data_index += 1
|
| 66 |
+
if data_index >= data_len:
|
| 67 |
+
break
|
| 68 |
+
return image,msg
|