Skip to content
Permalink
main
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
import cv2
import matplotlib.pyplot as plt
def decode_image(filename):
"""
Decode an image to check for hidden text using Least Significant Bit (LSB) Steganography.
This function allows the user to input an encoded image filename.
It loads the encoded image, decodes the hidden text within it using LSB Steganography,
and checks if there is hidden text in the image.
If hidden text is found, it prints the hidden text and indicates that the image has been tampered.
If no hidden text is found, it prints a message indicating that the image has not been tampered.
This can be extended by adding appropriate decryption algorithms to decode the hidden text.
:param filename - The name of the encoded file. It is in the format <>_encoded.PNG
:return String - The decoded information indicating whether image has been tampered or not.
"""
# Verifying if the file exists and is a valid image
try:
encoded_image = cv2.imread(filename) # Reading the image file
if encoded_image is None: # Checking if the image is valid or not
raise FileNotFoundError() # If not, raising an error
except FileNotFoundError:
return "Invalid image file." # Printing an error msg
# Decode the text within the image
message_bits = ""
for row in encoded_image:
for pixel in row:
message_bits += str(pixel[0] & 1)
message_bits += str(pixel[1] & 1)
message_bits += str(pixel[2] & 1)
# Convert binary message string to text
message = ""
for i in range(0, len(message_bits), 8):
byte = message_bits[i:i + 8]
data = chr(int(byte, 2))
if data == "!":
if message.endswith("!!"):
break
message += data
# Check if there is hidden text in the image
if not message.endswith("!!"):
return "The image has not been tampered and there is no hidden text in the image."
else:
hidden_text = message[:-2]
return "The image has been tampered and a text has been encoded in it. The Hidden Text is: {}".format(
hidden_text)
if __name__ == '__main__':
filename = input("Encoded Image Filename (with extension): ")
print(decode_image(filename))