Image Steganography using OpenCV in Python
Last Updated :
04 Jan, 2023
Image Steganography is the process of hiding secret data in some image. In this post, we will hide one image inside another and convert it into another image and then extract back both the images from the previous image.
The idea behind image-based Steganography is very simple. Images are composed of digital data (pixels), which describes what’s inside the picture, usually the colors of all the pixels. Since we know every image is made up of pixels and every pixel contains 3-values (red, green, blue).
For example, suppose we have to hide img2 in img1, where both img1 and img2 are
numpy nd
array of pixel values. The size of img2 must be less than the size of img1. We are using color images, hence both will have 3 values (red, green, blue). Each pixel value varies from 0 to 255, so each pixel value is of 1 byte or 8 bits. Let
img[i][j][l]
be the pixel value at location
(i, j)
and of channel l where i varies from 0 to width and j varies from 0 to height and l varies from 0 to 2.
Note: The quality of the new images is a little bit less than the old images.
Encoding
Let
img1[i][j][l]
and
img2[i][j][l]
be some pixel value of each image. Let v1 be 8 bits binary representation of
img1[i][j][l]
and v2 be 8 bits binary representation of
img2[i][j][l]
. Therefore,
v3=v1[:4]+v2[:4]
, where, v3 is the first 4 bits of v1 and v2. Then we assign
img1[i][j][l]
to v3.
Here img1 is the final image produced after encoding.
Decoding
Let
img[i][j][l]
be the pixel value of the image. Let v1 be 8 bits binary representation of
img[i][j][l]
. Let
v2=v1[:4]+4
random bits and
v3=v1[4:]+4
random bits. Then we assign
img1[i][j][l]
to v2 and
img2[i][j][l]
to v3.
Here img1 and img2 are the final images produced after decoding.
Implementation
Lets' consider the images used are as follows:
Image 1
Image 2

We want to hide image2 in image1. Below is the implementation.
Python3
# Python program to demonstrate
# image steganography using OpenCV
import cv2
import numpy as np
import random
# Encryption function
def encrypt():
# img1 and img2 are the
# two input images
img1 = cv2.imread('pic1.jpg')
img2 = cv2.imread('pic2.jpg')
for i in range(img2.shape[0]):
for j in range(img2.shape[1]):
for l in range(3):
# v1 and v2 are 8-bit pixel values
# of img1 and img2 respectively
v1 = format(img1[i][j][l], '08b')
v2 = format(img2[i][j][l], '08b')
# Taking 4 MSBs of each image
v3 = v1[:4] + v2[:4]
img1[i][j][l]= int(v3, 2)
cv2.imwrite('pic3in2.png', img1)
# Decryption function
def decrypt():
# Encrypted image
img = cv2.imread('pic3in2.png')
width = img.shape[0]
height = img.shape[1]
# img1 and img2 are two blank images
img1 = np.zeros((width, height, 3), np.uint8)
img2 = np.zeros((width, height, 3), np.uint8)
for i in range(width):
for j in range(height):
for l in range(3):
v1 = format(img[i][j][l], '08b')
v2 = v1[:4] + chr(random.randint(0, 1)+48) * 4
v3 = v1[4:] + chr(random.randint(0, 1)+48) * 4
# Appending data to img1 and img2
img1[i][j][l]= int(v2, 2)
img2[i][j][l]= int(v3, 2)
# These are two images produced from
# the encrypted image
cv2.imwrite('pic2_re.png', img1)
cv2.imwrite('pic3_re.png', img2)
# Driver's code
encrypt()
decrypt()
Output:
After Encryption:
After Decryption:
Image 1
Image 2
Similar Reads
Python Tutorial - Learn Python Programming Language Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly. It'sA high-level language, used in web development, data science, automation, AI and more.Known fo
10 min read
Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Python Introduction Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien
3 min read
Python Data Types Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
9 min read
Input and Output in Python Understanding input and output operations is fundamental to Python programming. With the print() function, we can display output in various formats, while the input() function enables interaction with users by gathering input during program execution. Taking input in PythonPython input() function is
8 min read
Enumerate() in Python enumerate() function adds a counter to each item in a list or other iterable. It turns the iterable into something we can loop through, where each item comes with its number (starting from 0 by default). We can also turn it into a list of (number, item) pairs using list().Let's look at a simple exam
3 min read