Image Steganography Demo - Jupyter Notebook
Image Steganography Demo - Jupyter Notebook
[1]:
import numpy as np
from PIL import Image
In [2]:
if img.mode == 'RGB':
n = 3
elif img.mode == 'RGBA':
n = 4
total_pixels = array.size//n
message += "$t3g0"
b_message = ''.join([format(ord(i), "08b") for i in message])
req_pixels = len(b_message)
else:
index=0
for p in range(total_pixels):
for q in range(0, 3):
if index < req_pixels:
array[p][q] = int(bin(array[p][q])[2:9] + b_message[index], 2)
index += 1
array=array.reshape(height, width, n)
enc_img = Image.fromarray(array.astype('uint8'), img.mode)
enc_img.save(dest)
print("Image Encoded Successfully")
In [3]:
def Decode(src):
if img.mode == 'RGB':
n = 3
elif img.mode == 'RGBA':
n = 4
total_pixels = array.size//n
hidden_bits = ""
for p in range(total_pixels):
for q in range(0, 3):
hidden_bits += (bin(array[p][q])[2:][-1])
message = ""
for i in range(len(hidden_bits)):
if message[-5:] == "$t3g0":
break
else:
message += chr(int(hidden_bits[i], 2))
if "$t3g0" in message:
print("Hidden Message:", message[:-5])
else:
print("No Hidden Message Found")
In [4]:
def Stego():
print("--Welcome to $t3g0--")
print("1: Encode")
print("2: Decode")
func = input()
if func == '1':
print("Enter Source Image Path")
src = input()
print("Enter Message to Hide")
message = input()
print("Enter Destination Image Path")
dest = input()
print("Encoding...")
Encode(src, message, dest)
else:
print("ERROR: Invalid option chosen")
In [11]:
Stego()
--Welcome to $t3g0--
1: Encode
2: Decode
cube.png
hellow world
cube-e.png
Encoding...
In [12]:
Stego()
--Welcome to $t3g0--
1: Encode
2: Decode
cube-e.png
Decoding...
In [ ]: