Image Steganography using Python
Hey there! In this tutorial, we will be learning to perform Image Steganography using Python.
Steganography is the process of hiding secretive data within an ordinary file during transmission. Both the secretive data and ordinary files can be in the form of a text message, image, audio clip, or video file.
Image steganography refers to hiding secretive data within an image or video file.
Below attached is a simple example of hiding a text message within an image. The two main steps involved are:
Encryption: Storing the text message within an image
Decryption: Extracting the message hidden within the image
- Firstly, open PyCharm and create a project titled Image_Steganography. Then, create two python files titled encryption.py and decryption.py.
- Now, save any image of your choice within this project. I have used an image titled photo.jpg.
- Open the terminal and type the below-listed commands to install the respective libraries.
pip install Pillow pip install stepic
- Pillow: A Python Imaging Library (PIL) that supports image processing capabilities such as opening, manipulating, and saving images of various formats.
- Stepic: A Python module used for hiding data within an image by slightly modifying the colors of the image. These modifications are usually indistinguishable by the human eye but are machine-distinguishable. Click here, to learn how this module performs data hiding.
Within the encryption.py file, type the below-specified Python code. Then, run the code to generate the results attached below.
# Import necessary libraries from PIL import Image import stepic # Open the image within which the secret message is to be stored: img = Image.open("photo.jpg") # Specify the secret message: message = "Hey there! Welcome to CodeSpeedy." # Convert the message into UTF-8 format: message = message.encode() # Pass the image and message into the encode() function. # This function returns a new image within which the message is hidden: encoded_img = stepic.encode(img, message) # Specify the name and extension for the new image generated: encoded_img.save("encrypted_photo.png") print("Encryption Completed!")
As seen in the above image, both the original image and the image obtained post encryption look the same. Neither is the secret text message hidden within the latter image visible to us, nor does this image look modified with respect to pixel intensities to the human eye.
Within the decryption.py file, type the below-specified code. Then, run the code to obtain the hidden secret message as shown below.
# Import necessary libraries from PIL import Image import stepic # Open the image from which the secret message is to be extracted: image = Image.open("encrypted_photo.png") # Pass the above image into the decode() function. # This function returns the secret message in the form of a string: decoded_msg = stepic.decode(image) print("Decryption Completed!") # Display the message print("Decoded Message:", decoded_msg)
Decryption Completed! Decoded Message: Hey there! Welcome to CodeSpeedy.
Also read: Extract images from a video in Python