Image Steganography

Shivam Chaudhary
3 min readJun 14, 2021

--

Banner vector created by starline — www.freepik.com

StegMe is an application which uses Image steganography technique to maintain the Message Secrecy…

Image Steganography is a technique in which we can hide the existence of a message into an Image. Most of the message can be of any type (eg:. Text, Audio, Video, Image etc.)

But here we’re going to focus on text data. We are going to use the most basic but effective technique i.e. LSB (Least Significant Bits). This is the simplest form of Image Steganography technique in which we first convert the data in the form of Binary strings and then we substitute the last bit of the each Image Pixel with the binary bits of Message data.

Right most side is the Least Significant Bit

Encode

At First we have to ask something to encode…

So first choose a message to encode and an image in which we are going to perform this technique. Convert them into byte string…

Here is my convert.py to convert normal text into binary string.

# to convert text to binary code list and a binary code list to text.
import random
class Text_to_binary:def __init__(self, text):
self.text = text
self.text += '`'
def convert_to_binary(self):
new_data = []
for i in self.text:
new_data.append(format(ord(i), '08b'))
return new_data

So now we gonna convert the Image into pixels byte string. We can use PIL for this. And then we can make changes to the Image pixels. In my code I have created another array (Numpy array) for this changes.

Now we just need to iterate all over the Pixels array and change the least significant bits. We can change maximum 2 least significant bits, so it won’t make a recognizable pattern change in that Image.

After encoding all the message into that image, copy the remaining pixels as it is in the new image pixel array and come out of the loop. This is how we can use this technique for hiding the existence of any type of the message. For the code to Encode things described above, Just follow this link to my github repo.

Decode

For decoding the we just need that Image we encoded before…

Add this Class into the convert.py file to convert Binary string to normal Text again.

class Binary_to_text:def __init__(self, data):
self.data = data
self.string = ""
def convert_to_text(self):for j in self.data:
#print(j)
if j != '':
try:
self.string += chr(int(j, 2))
except:
self.string += chr(0)
if int(j, 2) == 96: # First occurrence of '`' stops the iteration of the data
break
else:
j = random.choice(['0', '1'])
try:
self.string += chr(int(j, 2))
except:
self.string += chr(0)
if int(j, 2) == 96: # First occurrence of '`' stops the iteration of the data
break
self.string.rstrip('`')

return self.string

So now we have to reverse the process. And to do that we need to again convert that Image into an array of pixels. So we can just go through every pixels and extract the last 2 bits and then convert them to Normal readable text again. This is as simple as encoding…

So try it yourself.

For more help and full code for this Image Steganography Project just follow this link to my Github repo.

And if you want to use this functionality to do some real stuff or to try this project. Just check out the official StegMe website. This Web-application is developed using Django.

Thanks for reading this article. Hope you found this useful for your next project.

--

--

Shivam Chaudhary
Shivam Chaudhary

No responses yet