stegnography python code

Solutions on MaxInterview for stegnography python code by the best coders in the world

showing results for - "stegnography python code"
Jan
29 Apr 2016
1from tkinter import *
2from PIL import Image, ImageTk
3from tkinter import filedialog
4import cv2
5import numpy as np
6import math
7
8global path_image
9
10image_display_size = 300, 300
11
12def on_click():
13    # Step 1.5
14    global path_image
15    # use the tkinter filedialog library to open the file using a dialog box.
16    # obtain the image of the path
17    path_image = filedialog.askopenfilename()
18    # load the image using the path
19    load_image = Image.open(path_image)
20    # set the image into the GUI using the thumbnail function from tkinter
21    load_image.thumbnail(image_display_size, Image.ANTIALIAS)
22    # load the image as a numpy array for efficient computation and change the type to unsigned integer
23    np_load_image = np.asarray(load_image)
24    np_load_image = Image.fromarray(np.uint8(np_load_image))
25    render = ImageTk.PhotoImage(np_load_image)
26    img = Label(app, image=render)
27    img.image = render
28    img.place(x=20, y=50)
29
30def encrypt_data_into_image():
31    # Step 2
32    global path_image
33    data = txt.get(1.0, "end-1c")
34    # load the image
35    img = cv2.imread(path_image)
36    # break the image into its character level. Represent the characyers in ASCII.
37    data = [format(ord(i), '08b') for i in data]
38    _, width, _ = img.shape
39    # algorithm to encode the image
40    PixReq = len(data) * 3
41
42    RowReq = PixReq/width
43    RowReq = math.ceil(RowReq)
44
45    count = 0
46    charCount = 0
47    # Step 3
48    for i in range(RowReq + 1):
49        # Step 4
50        while(count < width and charCount < len(data)):
51            char = data[charCount]
52            charCount += 1
53            # Step 5
54            for index_k, k in enumerate(char):
55                if((k == '1' and img[i][count][index_k % 3] % 2 == 0) or (k == '0' and img[i][count][index_k % 3] % 2 == 1)):
56                    img[i][count][index_k % 3] -= 1
57                if(index_k % 3 == 2):
58                    count += 1
59                if(index_k == 7):
60                    if(charCount*3 < PixReq and img[i][count][2] % 2 == 1):
61                        img[i][count][2] -= 1
62                    if(charCount*3 >= PixReq and img[i][count][2] % 2 == 0):
63                        img[i][count][2] -= 1
64                    count += 1
65        count = 0
66    # Step 6
67    # Write the encrypted image into a new file
68    cv2.imwrite("encrypted_image.png", img)
69    # Display the success label.
70    success_label = Label(app, text="Encryption Successful!",
71                bg='lavender', font=("Times New Roman", 20))
72    success_label.place(x=160, y=300)
73
74# Step 1
75# Defined the TKinter object app with background lavender, title Encrypt, and app size 600*600 pixels.
76app = Tk()
77app.configure(background='lavender')
78app.title("Encrypt")
79app.geometry('600x600')
80# create a button for calling the function on_click
81on_click_button = Button(app, text="Choose Image", bg='white', fg='black', command=on_click)
82on_click_button.place(x=250, y=10)
83# add a text box using tkinter's Text function and place it at (340,55). The text box is of height 165pixels.
84txt = Text(app, wrap=WORD, width=30)
85txt.place(x=340, y=55, height=165)
86
87encrypt_button = Button(app, text="Encode", bg='white', fg='black', command=encrypt_data_into_image)
88encrypt_button.place(x=435, y=230)
89app.mainloop()
90