How to do Steganography in python

steagnography 1

How to do Steganography in python : You can hide text in a digital image in a way that is completely invisible to the naked eye. This technique is called watermarking. It’s actually steganography pure and simple.

How does it work?

A colour image can be described in computer terms as a triplet matrix. Each triplet gives the colour of a pixel in the RGB system. Today, an image is composed of several million pixels. This allows us to hide a large number of characters in these images.


one pixel
Representation of a pixel in RGB system

Integration of the message in the image :


Each character in the text to be hidden will be represented by its extended ASCII code. For example, the ASCII code of “A” is 64, which gives in binary, on one byte (8 bits): 01000001 . The complete text will therefore be a sequence of 0 and 1, each character using 8 bits and implicitly 8 pixels.

In the python code below we will work only on the red component of the pixel (we can of course use all components). The hidden message will be: “AMIRA DATA – All the data you need“. For the first 8 pixels of the image, we will hide the length of the string. It is 34 characters long (each space must be counted so that the original message is not altered), i.e. 00100010 in binary value. The next step is to convert our string to binary. We saw above that the ASCII code of the letter “A” was 64 which gives in binary 01000001.

pixel (red component)12345678910111213141516
Image originale231121022021313745181715416719371014038
Reduction230121022021303644181615416619261004038
Binary0010001001000001
Modified image230121032021303645181615516619261004039


In the table below, the second row is given the red component of the first 16 pixels. In a first step, in the reduction line, the values are reduced to the largest even number less than or equal to the value . Then, we will add to these even numbers 0 and 1 and it is these 0 and 1, grouped by 8, which will transmit the hidden information.


#-*- coding:Latin-1 -*-
from PIL import Image

# function to hide the message in the image 
def encode_message(directory,saved_image,message):
    im = Image.open(directory)
    w,h=im.size # we retrieve the dimensions of the image
    r,g,b=im.split() #Let's split the image in three (red green blue)
    r=list(r.getdata())  #we turn the image into a list

    u=len(message)
    v=bin(len(message))[2:].rjust(8,"0")    #we note the length of the string and transform it to binary
    ascii=[bin(ord(x))[2:].rjust(8,"0") for x in message]  #we transform the string into a list of 0 and 1
    a=''.join(ascii) #transformation of the list into a chain

    #the length of the list is encoded in the first 8 red pixels.
    for j in range(8):
        r[j]=2*int(r[j]//2)+int(v[j])

    #we code the string in the following pixels
    for i in range(8*u):
        r[i+8]=2*int(r[i+8]//2)+int(a[i])

    # we recreate the red image
    nr = Image.new("L",(w,h))
    nr.putdata(r)
    # merging the three new images
    imgnew = Image.merge('RGB',(nr,g,b))
    imgnew.save(saved_image)

Retrieving text from the image :


Recovery is a five-step process::

  1. Retrieve the matrix describing the image.
  2. Replace an even number by 0, an odd number by 1.
  3. Group the bits in groups of 8.
  4. Convert each byte to a decimal number.
  5. Write the characters corresponding to the ASCII codes obtained.
# function to retrieve the hidden message 
def decode_message(directory):
    im = Image.open(directory)
    r,g,b=im.split()
    r=list(r.getdata())
    # Reading the length of the string
    p=[str(x%2) for x in r[0:8]]
    q="".join(p)
    q=int(q,2)
    # Message reading
    n=[str(x%2) for x in r[8:8*(q+1)]]
    b="".join(n)
    message=""

    for k in range(0,q):
        l=b[8*k:8*k+8]
        message=message+chr(int(l,2))
    print (message)



I hope you were interested in How to do Steganography in python. Let us know what you think! 🙂

By ayed_amira

I'm a data scientist. Passionate about new technologies and programming I created this website mainly for people who want to learn more about data science and programming :)

1 comment

Leave a comment

Your email address will not be published.