Image based Steganography using Python?

Steganography is a technique of hiding information behind the scene. It’s is not like cryptography which focuses on encrypting data(through different algorithms like SHA1, MD5 etc), steganography focuses more on hiding the data (data can be a file, image, message or video) within another file, image, message or video to avoid any attraction.
So in this we’ll try to create a simple python program that hides the information behind the image without noticiable changes in the looks of the image. There are two main parts of the program – first is a decoding function that can extract secret information from an image file and second is a encoding function that will encode secret messages into images.
We use the Python Pillow library for this purpose (you can openCv or others too ☺). You can install it using pip, just run pip install pillow in your command prompt:
$pip install pillow
Pixels are the smallest individual element of an image. So, each pixel represents a part of the original image. It means, higher the pixel-higher or much accurate representations of the actual picture.
In a black and white image (not greyscale), black pixel has a value 1 and a white pixel as a value of 0. Whereas in colored images, they have three main color components(RGB- Red, Green, Blue), with pixel values of 0-255 for each pixel. So a pixel of (255, 255, 255) will represent a white and (0,0,0) means black. As the maximum number an 8-bit binary number can represent 255, is the maximum number we can go.
As the base of binary-number is 2, we can convert the binary number into decimal very easily. Let, our binary number is 01010101, then its equivalent decimal number(base 10) will be:
26 +24 + 22 + 20 = 64 + 16 + 4 + 1 = 85
You can test above – binary to decimal conversion in your python terminal too.
>>> print(0b01010101)
85
>>> type(0b01010101)
<class 'int'>
>>> 0b01010101
85
>>> 0b01010110
86
How we gona achieve it:
Step 1: Import the required library/package.
Step 2: Open the file or Image
Step 3: Encode some text into the source Image & then save it.
Step 4: Check both the images (with and without hidden data file) and see if there is any visible changes.
Step 5: Decode the image- to extract data from the image
Implementation of above steps:
>>> #Import the required library
>>> from PIL import Image
>>> import stepic
>>>
I have used the stepic library for encoding and decoding purpose. You can install the stepic library using pip:
>>> #Open Image or file in which you want to hide your data
>>> im = Image.open('TajMahal.png')
>>>
>>> #Encode some text into your Image file and save it in another file
>>> im1 = stepic.encode(im, b'Hello Python')
>>> im1.save('TajMahal.png', 'PNG')
>>>
>>> #Now is the time to check both images and see if there is any visible changes
>>> im1 = Image.open('TajMahal.png')
>>> im1.show()
Image with hidden text:
Actual image:
>>> im.show()
>>>'
>>>
>>> #Decode the image so as to extract the hidden data from the image
>>> im2 = Image.open('TajMahal.png')
>>> stegoImage = stepic.decode(im2)
>>> stegoImage
'Hello Python'
So we see how easy is to hide the text behind the image. You can use other input items like video or other formats like jpeg and you can use other libraries to give you the same results, Happy Steganography with Python ☺.