Search code examples
pythonimagepython-imaging-librarysteganography

Hide a message in the least significant image bit of a greyscale image


I need to read in a message from the user, iterate across each bit of the message and overwrite the least significant image bit with each message bit.

I currently have a program that iterates over each image pixel and just rewrites out the image.

So how can i read the least significant image bit and how can i get each message bit.

This is for a grey scale image using python 3.x

from PIL import Image

import numpy as np
import bitget
import scipy.misc as smp

im = Image.open("catBlack.png") #Can be many different formats.
pix = im.load()
print (im.size )#Get the width and hight of the image for iterating over
print (pix[1,1]) #Get the RGBA Value of the a pixel of an image
#pix[x,y] = value # Set the RGBA Value of the image (tup


data = np.zeros( (im.size[0],im.size[1],2), dtype=np.uint8 )

for i in range (im.size[0]):
    for j in range (im.size[1]):
        print (i,j)
        data[i,j] = pix[i,j]

im = Image.fromarray(data)
im.save("GreyCat.png")

Also , how would i decode out this message

Cheers for the help


Solution

  • After reading the message you can convert it into ascii code (ascii range exists between 0 and 127) i.e you will need max. 7 bits to represent the letter

    Gray scale can have value from 0 to 255 and each pixel has 8 bits so you can use 2 last bits of 4 pixels to represent one word.

    e.g You want to transmit hi

    ascii code of h - 73 binary of 73 - 1001001 ascii code of i - 74 ascii code of i -1001010

    • now you will iterate through each pixel like you mentioned
    • read the color value of pixel convert it into binary

    suppose color value is

    1111111 for first pixel
    0101010 for second
    1100110 for third
    1111111 for fourth
    0001111 for fifth
    1011111 for sixth
    1100110 for seventh
    1110001 for eighth
    1111111 for ninth
    1010101 for tenth

    first we have to transmit h (01001001) so we will change last two bits of 4 pixels new values of color will be something like this

    1111101 for first pixel
    0101000 for second
    1100110 for third
    1111101 for fourth

    now for i (01001010) values will be like

    0001101 for fifth
    1011100 for sixth
    1100110 for seventh
    1110010 for eighth

    now we will change remaining pixels last bits to all zeros
    1111100 for ninth
    1010100 for tenth