Python Image Steganography — Invisible Narratives

Rahul Patodi
Wiki Flood
Published in
4 min readDec 21, 2023

About Python Image Steganography Project-

In this Python Project, we are going to build an Image Steganography Application in Python. It is a GUI-based application made with the Tkinter library. The PIL library is used for image processing. The LSB (least significant bit) from the stegano library is used for hiding and retrieving the text in an image.

In this application, users can hide text in any image and also retrieve it using the same application.

Prerequisites for Python Image Steganography:

  • Initially, we’ll use the pip installer to install the necessary library on our system.
  • This project necessitates a solid understanding of Python and proficiency in using tkinter to develop the GUI for the application.
pip install tkinter

The following Steps are Required to Build Project:

  1. Importing the libraries and modules.
  2. Initializing GUI window.
  3. Creating Labels, Frames and Text widget.
  4. Creating Buttons.
  5. Implementing the Open file function.
  6. Implementing the Save file function.
  7. Implementing the Hide text function.
  8. Implementing the Show text function.

1. Importing the libraries and modules:

from tkinter import *
from tkinter import filedialog
from PIL import Image, ImageTk
import os
from stegano import lsb

2. Initializing GUI window- We will initiate the GUI window for the application.

root = Tk()
root.geometry("700x500")
root.title("Image Steganography Application")
root.config(bg='#BCD2EE')

root- It is the name of our GUI window.

Tk()- Initialised tkinter which means GUI window created.

geometry()- This method provides the length and breadth to the GUI window.

title()- This method gives title to the window

confg()- This method sets the configuration of the window.

3. Creating Labels, Frames and Text widget- We will create the frame for the labels in which the desired image will be displayed.

Label(root, text="Image Steganography-PythonFlood", font=('Arial,bold', 22),
bg='#6E7B8B', fg='White', width=45).pack(pady=10)


f1 = Frame(root, width=320, height=260, bg='Black', bd=5)
f1.place(x=20, y=80)


lb1 = Label(f1, bg="Black")
lb1.place(x=40, y=10)


f2 = Frame(root, width=320, height=260, bg='White', bd=5)
f2.place(x=360, y=80)


txt = Text(f2, font=('Arial,bold', 15), wrap=WORD)
txt.place(x=0, y=0, width=310, height=250)

Label()- It is used to display one line or more than one line of text.

text- It is used to display text on labels.

font- It is a style in which font is written.

bg- It is the background Colour of the label.

fg- it is the colour of characters in labels.

pack()- This displays the widgets in the GUI window.

pady- It provides padding between widgets y-axis.

Frame- It is a container inside which the desired widgets are positioned.

Text- This widget is used to write multiline text.

wrap- It is used to prevent the overflow of text at the end of the line.

4. Creating Buttons- Creating a set button for opening and saving the image.Creating another set of buttons for hiding and showing the text in image.

btn1 = Button(root, text="Open Image", font=('Arial,bold', 15), bd=5, bg='#6E7B8B', fg='White', command=openfile)
btn1.place(x=30, y=400)
btn2 = Button(root, text="Save Image", font=('Arial,bold', 15), bd=5, bg='#6E7B8B', fg='White', command=save)
btn2.place(x=190, y=400)
btn3 = Button(root, text="Hide Text", font=('Arial,bold', 15), bd=5, bg='#6E7B8B', fg='White', command=hide)
btn3.place(x=380, y=400)
btn4 = Button(root, text="Show Text", font=('Arial,bold', 15), bd=5, bg='#6E7B8B', fg='White', command=show)
btn4.place(x=520, y=400)


root.mainloop()

Button()- It is a button used to display on our window.

bd- It is the border of the Button.

command- It is used as a function of a button when it is clicked.

place()- It is used to set the position.

root.mainloop()- This method will execute what we wish to execute in an application and end the mainloop in Python program.

5. Implementing the Open file function- This function will open the image in the application.

def openfile():
global file
file = filedialog.askopenfilename(initialdir=os.getcwd(), title="Select Image",
filetypes=(("PNG file", "*.png"),
("JPG file", "*.jpg"), ("All Types", "*.txt")))
img = Image.open(file)
img = ImageTk.PhotoImage(img)
lb1.config(image=img, width=250, height=250)
lb1.image = img

global- It is used to declare a global variable above all the functions.

filedialog.askopenfilename()- This is used to open the filedialog box for selecting the image.

Image.open()- It is used to open the Image in the application.

6. Implementing the Save file function- This function will save the image.

def save():
secret.save("Hidden.png")

save()- This method is used to save the file with the desired name.

7. Implementing the Hide text function- This function will hide the text in the opened image.

def hide():
global secret
message = txt.get(1.0, END)
secret = lsb.hide(str(file), message)

get()- It is used to retrieve the message from the Text widget which user wants to hide.

lsb.hide()- This is used to hide the text in the image.

8. Implementing the Show text function- This function will show the text hidden in the image.

def show():
clear = lsb.reveal(file)
txt.delete(1.0, END)
txt.insert(END, clear)

lsb.reveal()- This method is used to retrieve the hidden text from the Image.

delete()- It is used to delete the text from the Text widget.

insert()- It is used to insert the hidden text in the Text widget to display it to the user.

Python Image Steganography Output:

Conclusion:

We have successfully created an application that can hide data in any image and, at the same time, can also retrieve it. We learnt how to use the tkinter library for making the interface of application and PIL library for image processing. Now you can use this application to send secret message to your friends in any image of your choice.

--

--