imageSecure_suite / stegnography.py
RYU-KASH's picture
Upload 5 files
319dbb7 verified
import streamlit as st
from PIL import Image
import io
def steganography_function():
# Steganography functions
def encode_image(input_image, secret_message):
"""Encodes a secret message into an image."""
img = Image.open(input_image)
encoded = img.copy()
width, height = img.size
pixels = encoded.load()
# Convert the message into binary and add a delimiter
binary_message = ''.join(format(ord(char), '08b') for char in secret_message) + '1111111111111110'
data_index = 0
for y in range(height):
for x in range(width):
if data_index < len(binary_message):
pixel = list(pixels[x, y])
# Modify the LSB of each RGB value
for i in range(3): # Process R, G, B channels
if data_index < len(binary_message):
pixel[i] = (pixel[i] & ~1) | int(binary_message[data_index])
data_index += 1
pixels[x, y] = tuple(pixel)
output = io.BytesIO()
encoded.save(output, format='PNG')
output.seek(0)
return output
def decode_image(encoded_image):
"""Decodes a secret message from an image."""
img = Image.open(encoded_image)
width, height = img.size
pixels = img.load()
binary_message = ""
for y in range(height):
for x in range(width):
pixel = pixels[x, y]
for i in range(3): # Process R, G, B channels
binary_message += str(pixel[i] & 1)
# Split binary string into 8-bit chunks
message = ""
for i in range(0, len(binary_message), 8):
byte = binary_message[i:i+8]
if byte == '11111110': # Stop at the delimiter
break
message += chr(int(byte, 2))
return message
# Streamlit App
st.title("Steganography Tool")
st.subheader("Hide and retrieve secret messages in images")
# Tabs for Encode and Decode
tab1, tab2 = st.tabs(["πŸ”’ Encode Message", "πŸ”“ Decode Message"])
with tab1:
st.header("Encode a Secret Message")
uploaded_image = st.file_uploader("Upload an image to encode", type=["png", "jpg", "jpeg"])
if uploaded_image:
st.image(uploaded_image, caption="Selected Image for Encoding", use_column_width=True)
secret_message = st.text_area("Enter the secret message to hide")
encode_button = st.button("Apply Encoding")
if encode_button:
if uploaded_image and secret_message:
try:
encoded_image = encode_image(uploaded_image, secret_message)
st.success("Message encoded successfully!")
st.image(encoded_image, caption="Encoded Image", use_column_width=True)
st.download_button(
label="Download Encoded Image",
data=encoded_image,
file_name="encoded_image.png",
mime="image/png"
)
except Exception as e:
st.error(f"An error occurred: {e}")
else:
st.error("Please upload an image and enter a secret message.")
with tab2:
st.header("Decode a Secret Message")
encoded_image = st.file_uploader("Upload an image to decode", type=["png", "jpg", "jpeg"], key="decode")
if encoded_image:
st.image(encoded_image, caption="Selected Image for Decoding", use_column_width=True)
decode_button = st.button("Apply Decoding")
if decode_button:
if encoded_image:
try:
decoded_message = decode_image(encoded_image)
st.success("Message decoded successfully!")
st.text_area("Decoded Message", decoded_message, height=100)
except Exception as e:
st.error(f"An error occurred: {e}")
else:
st.error("Please upload an image to decode.")