Saturday, November 14, 2020

Steganography using Python with tkinter

 https://www.section.io/engineering-education/steganography-in-python/




# pip install opencv-python

from tkinter import *

from PIL import Image, ImageTk

from tkinter import filedialog

import cv2

import numpy as np

import math


global path_image


image_display_size = 300, 300


def OnClick():

  global path_image

  path_image = filedialog.askopenfilename()

  load_image = Image.open(path_image)

  load_image.thumbnail(image_display_size, Image.ANTIALIAS)

  np_load_image = np.asarray(load_image)

  np_load_image = Image.fromarray(np.uint8(np_load_image))

  render = ImageTk.PhotoImage(np_load_image)

  img = Label(app, image=render)

  img.image = render

  img.place(x=20, y=50)


def Encrypt():

  global path_image

  data = txt.get(1.0, "end-1c")

  img = cv2.imread(path_image)

  data = [format(ord(i), '08b') for i in data]    # break image to binary chararacters

  _, width, _ = img.shape

  PixReq = len(data) * 3                 # algorithm to encode the image


  RowReq = PixReq/width

  RowReq = math.ceil(RowReq)


  count = 0

  charCount = 0

  for i in range(RowReq + 1):

    while(count < width and charCount < len(data)):

      char = data[charCount]

      charCount += 1

      for index_k, k in enumerate(char):

        if((k == '1' and img[i][count][index_k % 3] % 2 == 0) or (k == '0' and img[i][count][index_k % 3] % 2 == 1)):

          img[i][count][index_k % 3] -= 1

        if(index_k % 3 == 2):

          count += 1

        if(index_k == 7):

          if(charCount*3 < PixReq and img[i][count][2] % 2 == 1):

            img[i][count][2] -= 1

          if(charCount*3 >= PixReq and img[i][count][2] % 2 == 0):

            img[i][count][2] -= 1

          count += 1

    count = 0


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


  success_label = Label(app, text="Encrypted!", bg='lavender', font=("Helvetica", 20))

  success_label.place(x=130, y=360)


app = Tk()

app.configure(background='lavender')

app.title("Encrypt")

app.geometry('400x400')


btnSelect = Button(app, text="Select PNG Image (Max 500 x 300)", command=OnClick)

btnSelect.place(x=100, y=10)


txt = Text(app, wrap=WORD, width=30)

txt.place(x=80, y=220, height=100)


encrypt_button = Button(app, text="Encode", bg='white', fg='black', command=Encrypt)

encrypt_button.place(x=180, y=328)


app.mainloop()

#############################################################################




import cv2
from tkinter import filedialog, Tk, Button, Label
from PIL import Image, ImageTk
import numpy as np

image_display_size = 500, 350

def decrypt():
  load = Image.open("EncryptedImage.png")
  load.thumbnail(image_display_size, Image.ANTIALIAS)
  load = np.asarray(load)
  load = Image.fromarray(np.uint8(load))
  render = ImageTk.PhotoImage(load)
  img = Label(app, image=render)
  img.image = render
  img.place(x=100, y=50)

  img = cv2.imread("EncryptedImage.png")
  data = []
  stop = False
  for index_i, i in enumerate(img):
    i.tolist()
    for index_j, j in enumerate(i):
      if((index_j) % 3 == 2):
        data.append(bin(j[0])[-1])  # first pixel
        data.append(bin(j[1])[-1])  # second pixel
        if(bin(j[2])[-1] == '1'):   # third pixel
          stop = True
          break
      else:
        data.append(bin(j[0])[-1])  # first pixel
        data.append(bin(j[1])[-1])  # second pixel
        data.append(bin(j[2])[-1])  # third pixel
    if(stop):
      break

  message = []
  for i in range(int((len(data)+1)/8)):  # join bits to form letters
    message.append(data[i*8:(i*8+8)])
  message = [chr(int(''.join(i), 2)) for i in message]  # join all letters to form the message.
  message = ''.join(message)
  message_label = Label(app, text=message, bg='lavender', font=("Times New Roman", 10))
  message_label.place(x=30, y=400)

app = Tk()
app.configure(background='lavender')
app.title("Decrypt")
app.geometry('600x600')

main_button = Button(app, text="Decrypt", bg='white', fg='black', command=decrypt)
main_button.place(x=250, y=10)
app.mainloop()





No comments:

Post a Comment