The Art of Hiding Secret Messages in Images with Python Steganography

Dayanand Shah
3 min readMar 5, 2023

--

When it comes to secure communication, encryption is a common technique used to protect the confidentiality of messages. However, encryption can sometimes attract attention and raise suspicion. An alternative method is a steganography, which involves hiding a message or information within another message or file, making it less likely to be detected. One form of steganography is image steganography, where the message is hidden within the pixels of an image.

In this blog, we will explore how to perform image steganography using Python. We will discuss the steps involved in encoding a message into an image and decoding the hidden message from the steganography image.

Steganography

Steganography is the practice of hiding a message or information within another message or file, and image steganography involves hiding information within the pixels of an image.

The process of image steganography involves encoding a message into the least significant bits of the color channels (red, green, and blue) of each pixel in the image. This can be achieved by iterating over each pixel of the image, extracting the least significant bit of each color channel, and modifying it to encode the binary message.

To decode the hidden message from the steganography image, we can reverse the process by iterating over each pixel of the stego image, extracting the least significant bit of each color channel, and concatenating them to form the binary message. We then convert this binary message back into its original text format to obtain the hidden message.

Let’s look into Python implementation.

Encode the secret message into an Image.

#Import the necessary libraries
from PIL import Image

#Open the image that you want to use for steganography
img = Image.open('image.png')

#Convert the image to RGB format:
img = img.convert('RGB')

#Get the size of the image:
width, height = img.size

#Define the secret message that you want to hide in the image:
message = 'FBI is looking for you.'

#Convert the message to binary:
binary_message = ''.join(format(ord(i), '08b') for i in message)

#Check that the length of the binary message is less than or equal to the number of pixels in the image:
if len(binary_message) > width * height:
raise ValueError('Message is too long to be hidden in the image')

#Iterate over each pixel in the image and modify the least significant bit of each color channel (red, green, and blue) to encode the binary message:
i = 0
for x in range(width):
for y in range(height):
r, g, b = img.getpixel((x, y))
if i < len(binary_message):
r = r & ~1 | int(binary_message[i])
i += 1
if i < len(binary_message):
g = g & ~1 | int(binary_message[i])
i += 1
if i < len(binary_message):
b = b & ~1 | int(binary_message[i])
i += 1
img.putpixel((x, y), (r, g, b))

#Save the encoded(modified) image:
img.save('steganography_image.png')

Decode the secret message from the Image.

#Open the steganography image that contains the hidden message:
stego_img = Image.open('steganography_image.png')

#Convert the stego image to RGB format:
stego_img = stego_img.convert('RGB')

#Get the size of the stego image:
width, height = stego_img.size

#Iterate over each pixel in the stego image and extract the least significant bit of each color channel (red, green, and blue) to decode the binary message:
binary_message = ''
i = 0
while True:
x, y = i % width, i // width
r, g, b = stego_img.getpixel((x, y))
binary_message += str(r & 1) + str(g & 1) + str(b & 1)
i += 1
if len(binary_message) % 8 == 0 and chr(int(binary_message[-8:], 2)) == '\x00':
break

#Convert the binary message to its original text format:
message = ''
for i in range(0, len(binary_message), 8):
message += chr(int(binary_message[i:i+8], 2))

#Print the decoded message:
print(message)

Note: The decoding process involves iterating over each pixel of the stego image, so it can be slow for large images or long messages. It’s also important to make sure that the stego image being decoded has actually been used for steganography and contains a hidden message.

That’s all for today. If you found this information helpful and would like to stay updated with more valuable content, please consider following me here. I strive to provide high-quality content and keep our readers informed of the latest trends and developments in the field. Thank you for your support!

--

--

Dayanand Shah
Dayanand Shah

No responses yet