Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Image based Steganography using Python?
Steganography is a technique of hiding information behind the scene. Unlike cryptography which focuses on encrypting data through algorithms like SHA1 or MD5, steganography focuses on hiding data (files, images, messages, or videos) within another medium to avoid detection.
In this tutorial, we'll create a Python program that hides information behind an image without noticeable changes to the image appearance. The program has two main components ? an encoding function to embed secret messages into images, and a decoding function to extract hidden information.
Installation
We'll use the Python Pillow library and the stepic library for steganography operations ?
pip install pillow stepic
Basic Concepts of Pixels and Color Models
Pixels are the smallest individual elements of an image. Each pixel represents a portion of the original image ? higher pixel count means more accurate representation.
In black and white images, pixels have binary values: 1 for black and 0 for white. In colored images, pixels use the RGB color model (Red, Green, Blue) with values ranging from 0-255 for each component. A pixel value of (255, 255, 255) represents white, while (0, 0, 0) represents black.
Binary to Decimal Conversion
Since 8-bit binary numbers can represent values up to 255, we can convert binary to decimal easily. For example, binary 01010101 converts to decimal as ?
2? + 2? + 2² + 2? = 64 + 16 + 4 + 1 = 85
You can verify this in Python ?
print(0b01010101) print(type(0b01010101)) print(0b01010110)
85 <class 'int'> 86
Implementation Steps
Our steganography program follows these steps ?
Step 1: Import required libraries Step 2: Open the source image Step 3: Encode text into the image and save it Step 4: Compare original and encoded images visually Step 5: Decode the image to extract hidden data
Complete Steganography Program
Importing Libraries
from PIL import Image import stepic
Encoding Hidden Message
First, we'll open an image and encode a secret message into it ?
# Open the source image
original_image = Image.open('source_image.png')
# Encode secret message into the image
encoded_image = stepic.encode(original_image, b'Hello Python')
# Save the image with hidden message
encoded_image.save('encoded_image.png', 'PNG')
Visual Comparison
Let's display both images to verify there are no visible changes ?
# Display the encoded image
encoded_display = Image.open('encoded_image.png')
encoded_display.show()
# Display the original image for comparison
original_image.show()
Decoding Hidden Message
Now we'll extract the hidden message from the encoded image ?
# Open the encoded image
stego_image = Image.open('encoded_image.png')
# Extract the hidden message
hidden_message = stepic.decode(stego_image)
print("Hidden message:", hidden_message)
Hidden message: Hello Python
How LSB Steganography Works
The stepic library uses Least Significant Bit (LSB) steganography. It modifies the least significant bits of pixel values to store message bits. Since LSB changes are minimal (±1), they're imperceptible to human eyes but can store substantial data.
Key Advantages
? Invisibility: Hidden data is undetectable to casual observation
? Simplicity: Easy implementation with existing Python libraries
? Versatility: Works with various image formats (PNG, JPEG, BMP)
? Capacity: Can hide significant amounts of text data
Conclusion
Steganography provides an effective method for hiding information within images using Python. The stepic library simplifies LSB steganography, allowing you to embed and extract secret messages without visible image changes. This technique has applications in secure communication, digital watermarking, and data protection.
