Skip to content

Given any image file input.png,You should hide a QR code in it, not visible to naked eyes, and generate the image file output as output.png, where the QR code image is generated by the program using the secret code user input (secret code can be any random string of length 6-14 example "my_secret"). Then to write another program to extract the Q…

Notifications You must be signed in to change notification settings

aks861999/Steganography_With_PyPy_Akash

Repository files navigation

Steganography_With_Python

Given any image file input.png,You should hide a QR code in it, not visible to naked eyes, and generate the image file output as output.png, where the QR code image is generated by the program using the secret code user input (secret code can be any random string of length 6-14 example "my_secret"). Then to write another program to extract the QR code from the image.

First of all we need to understand 'Image' is nothing but combination of numerous array.The dimension of the array obviously depends of the type of pixel in the image, if the picture is grayscale, indexed, or binary image is 2D. A true color, RGB image is 3D. This concept is very crucial in every step while analysing an 'image' file.

Generate QR code if given a secret code

Let's generate a QR code to embed a secured message in it. To do that you need to first understand what exactly is a QR code. QR code is a type of matrix barcode that is machine readable optical label which contains information about the item to which it is attached. In practice, QR codes often contain data for a locator, identifier, or tracker that points to a website or application, etc.

First you need to install qrcode and OpenCV libraries.

pip install opencv-python qrcode

import the required module

import qrcode

Now set a object called data to be hidden in QR Code

#data = "https://bytescare.com/"
data = input("Please enter the Security Code")

Set output file name

filename = "site.png"

Generate qr code and Build an Image type object

img = qrcode.make(data)

save image to a file in the same directory

img.save(filename)

Here is the generated image of QR Code sample

Please check here for full code.

Image Steganography

Hiding QR code in the image of your choice

Encoding

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.

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') 

Here is the generated output image sample Get the full code here

Decoding

In this part we have to extract site.png

sample

from the outout.png

sample

Let img[i][j][l] be the pixel value of the image outout.png. Let v1 be 8 bits binary representation of img[i][j][l]. Let v2=v1[:4]+4 random bits.

img = cv2.imread('output.png') 
    width = img.shape[0] 
    height = img.shape[1] 
      
    # img1 is a blank image #
    img1 = np.zeros((width, height, 3), np.uint8)

Then we assign img1[i][j][l] to v2.

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
                
                # Appending data to img1 #
                img1[i][j][l]= int(v2, 2) 

After following this part you sould have got this like of image as decrypted.png

sample

You can get the full code here

Extract The Secret Code From QR Code

Now you are at the verge of success and good to extract the sectret code what you have embedded recently.

Let's get started....

You have to first read the decrypted.pngc file.

img = cv2.imread("decrypted.png")

declare an object detector as

detector = cv2.QRCodeDetector()

Now detect and decode as

data, bbox, straight_qrcode = detector.detectAndDecode(img)

If there is any QR code in decrypted.pngc then store the extracetd data in object secret_text and print it and you should have got the secret text printed.

secret_text=data
print(f"The Secret Text is:\n{data}")

Now to identify the QR code visually you have to display the image with lines length of bounding box.

n_lines = len(bbox)
    for i in range(n_lines):
        # draw all lines
        point1 = tuple(bbox[i][0])
        point2 = tuple(bbox[(i+1) % n_lines][0])
        cv2.line(img, point1, point2, color=(255, 0, 0), thickness=2)

Save the decrypted QR code as decrypted_QR.png

cv2.imwrite("decrypted_QR.png",img)

Kudos and Congratulations!!!!!! you have got your Decrypted QR code. Now display it to visually affirm your Job.

cv2.imshow("img", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

sample

About

Given any image file input.png,You should hide a QR code in it, not visible to naked eyes, and generate the image file output as output.png, where the QR code image is generated by the program using the secret code user input (secret code can be any random string of length 6-14 example "my_secret"). Then to write another program to extract the Q…

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages