Image Steganography — The art of hiding messages inside images.

Aathil Ahamed
4 min readOct 3, 2019

In this article we are going to look about Image steganography and do some practices using Python.

Summary:

I. What is Steganography

II. Digital Image

III. Pixel theory

IV. Encoding

V. Decoding

What is Steganography

— — — — — — — — — — -

According to Wikipedia Steganography is defined as,

Steganography is the practice of concealing a file, message, image, or video within another file, message, image, or video.” — Wikipedia

and the word Steganography combines the Greek words steganos (στεγᾰνός), meaning “covered or concealed”, and graphe (γραφή) meaning “writing”.

The steganography technique is used by humans for so many years mostly in World war I and World War II period, they used some kind of techniques like stenography to share their secrets among their Army or friendship countries.

Nowadays after the digital steganography ideas was introduced it is mostly used by security forces and hackers to share their data without no one knowing.

Digital Image

— — — — —

First of all we will learn something about digital image,

According to Wikipedia digital image defined as,

A digital image is a numeric representation, normally binary, of a two-dimensional image. Depending on whether the image resolution is fixed, it may be of vector or raster type. By itself, the term “digital image” usually refers to raster images or bitmapped images (as opposed to vector images).

In here we are referencing digital images to the “raster graphics”, which are basically a dot matrix data structure, representing a grid of pixels,

Raster Graphics

Pixel Theory

— — — — — -

Images are made up of Pixels and every pixels contains three values,(We are working with RGB color model), Red-R, Green-G, Blue-B, each RGB value is 8-bit and the rightmost bits are less significant and leftmost bits are most significant bit.

If we change the leftmost bits it will highly impact on the final result, but if we change the rightmost bits it will have a small impact on the final result. This is the steganography key to hide a secret message inside a image.

Encoding

— — — — —

Every byte of data is converted to its 8-bit binary code using ASCII values. Now pixels are read from left to right in a group of 3 containing a total of 9 values. The first 8-values are used to store the binary data. The value is made odd for 1 and even for 0. we need to check to make sure there is sufficient space to store the message within the carrier.

Decoding

— — — —

Till the message is over, three pixels are read at a time which means last value is odd. Every 3-pixels contain a binary data, which can be extracted by the same encoding logic.

Start working

— — — — — -

We will use PIL library (python) here to get some information such as the image type and size to carry on the process.

  1. Import PIL and converting data
#we have to import Image from PIL library, as I sadi earlier PIL used to extract the details of image, 
#specially pixels of image
from PIL import Image
def genrateData(payload_data):

newData = []

for i in payload_data:
newData.append(format(ord(i), ‘08b’))
return newData

if you don’t have the PIL library on your machine you have to install by using this command.

pip install pillow

2. Modify the pixels and Putting modified pixels in the new image , in here Pixel value should be made odd for 1 and even for 0

#def modPix(pixel, payload_data): 
datalist = genrateData(payload_data)
lendata = len(datalist)
imdata = iter(pixel)

for i in range(lendata):
pixel = [value for value in imdata.__next__()[:3] + imdata.__next__()[:3] +
imdata.__next__()[:3]]
for j in range(0, 8):
if (datalist[i][j]==’0') and (pixel[j]% 2 != 0):

if (pixel[j]% 2 != 0):
pixel[j] -= 1

elif (datalist[i][j] == '1') and (pixel[j] % 2 == 0):
pixel[j] -= 1
if (i == lendata - 1):
if (pixel[-1] % 2 == 0):
pixel[-1] -= 1
else:
if (pixel[-1] % 2 != 0):
pixel[-1] -= 1
pixel = tuple(pixel)
yield pixel[0:3]
yield pixel[3:6]
yield pixel[6:9]
def encode_enc(newImage, payload_data):
w = newImage.size[0]
(x, y) = (0, 0)

for pixel in modPix(newImage.getdata(), payload_data):

#Putting modified pixels in the new image
newImage.putpixel((x, y), pixel)
if (x == w - 1):
x = 0
y += 1
else:
x += 1

3. Encoding the data

def encode(): 
img = input("Enter the image name(Eg: A.png): ")
image = Image.open(img, 'r')

payload_data = input("Enter Your Message : ")
if (len(payload_data) == 0):
raise ValueError('Please type your Message')

newImage = image.copy()
encode_enc(newImage, payload_data)

new_img_name = input("Enter a name for new image(b.png): ")
newImage.save(new_img_name, str(new_img_name.split(".")[1].upper()))

4. Decoding the data

def decode():
img = input("Enter image name to decode (b.png) :")
image = Image.open(img, 'r')

payload_data = ''
secretData = iter(image.getdata())

while (True):
pixels = [value for value in secretData.__next__()[:3] +
secretData.__next__()[:3] +
secretData.__next__()[:3]]
# string of binary payload_data
binstr = ''

for i in pixels[:8]:
if (i % 2 == 0):
binstr += '0'
else:
binstr += '1'

payload_data += chr(int(binstr, 2))
if (pixels[-1] % 2 != 0):
return payload_data

Our file is ready, you can use it ;)

Full Code:

Thank you so much for reading, let you know your thoughts..!

--

--