Python Coding for Final Year Project
Python Coding for Final Year Project
AND SECURITY OF
ENCRYPTION ALGORITHM
BASED ON IMAGE
PROCESSING
PYTHON CODING:
import tkinter as tk
from tkinter import filedialog, messagebox, Label, Button, Frame
from PIL import ImageTk, Image
import cv2
import numpy as np
import os
class ImageEncryptorApp:
def __init__(self, master):
self.master = master
self.master.title("Image Encryption Decryption")
self.master.attributes('-fullscreen', True)
self.master.bind("<Escape>", self.exit_fullscreen)
self.username = ""
self.unique_key = ""
self.is_sender = None
self.img_path = None
self.original_image = None
self.encrypted_img_path = 'output/image_encrypted.jpg'
self.init_login_screen()
def init_login_screen(self):
self.clear_frame()
self.login_frame = Frame(self.master)
self.login_frame.pack()
def clear_frame(self):
for widget in self.master.winfo_children():
widget.destroy()
def exit_app(self):
self.master.quit() # Exit the application
def login_user(self):
username = self.username_entry.get()
password = self.password_entry.get()
unique_key = self.key_entry.get()
if username in users and users[username][0] == password and
users[username][1] == unique_key:
self.username = username
self.unique_key = unique_key
self.init_main_screen()
else:
messagebox.showerror("Error", "Invalid credentials or unique key!")
def register_user(self):
username = self.username_entry.get()
password = self.password_entry.get()
unique_key = self.key_entry.get()
if username in users:
messagebox.showerror("Error", "User already exists!")
else:
users[username] = (password, unique_key)
messagebox.showinfo("Success", "Registration successful!")
def init_main_screen(self):
self.clear_frame()
self.main_frame = Frame(self.master)
self.main_frame.pack()
def sender_mode(self):
self.is_sender = True
self.init_image_screen()
def receiver_mode(self):
self.is_sender = False
self.init_image_screen()
def init_image_screen(self):
self.clear_frame()
Label(self.master, text="Image Encryption/Decryption", font=("Times
New Roman", 40)).pack(pady=20)
self.image_label = Label(self.master)
self.image_label.pack(pady=10)
if self.is_sender:
Button(self.master, text="Choose Image", command=self.open_img,
font=("Times New Roman", 20)).pack(pady=10)
Button(self.master, text="Encrypt", command=self.en_fun,
font=("Times New Roman", 20)).pack(pady=10)
else:
self.display_encrypted_image()
Button(self.master, text="Decrypt", command=self.de_fun,
font=("Times New Roman", 20)).pack(pady=10)
def open_img(self):
self.img_path = filedialog.askopenfilename(title='Open Image',
initialdir='input')
if self.img_path:
img = Image.open(self.img_path)
img.thumbnail((400, 400))
img_display = ImageTk.PhotoImage(img)
self.image_label.configure(image=img_display)
self.image_label.image = img_display
self.image_label['text'] = 'Chosen Image:'
self.original_image = img
os.replace(self.img_path, f'input/{os.path.basename(self.img_path)}')
self.img_path = f'input/{os.path.basename(self.img_path)}'
def en_fun(self):
if not self.img_path:
messagebox.showerror("Error", "No image selected!")
return
image_input = cv2.imread(self.img_path, 0)
(x1, y) = image_input.shape
image_input = image_input.astype(float) / 255.0
mu, sigma = 0, 0.1
key = np.random.normal(mu, sigma, (x1, y)) + np.finfo(float).eps
image_encrypted = image_input / key
img_enc = Image.open(self.encrypted_img_path)
img_enc.thumbnail((400, 400))
img_enc_display = ImageTk.PhotoImage(img_enc)
self.image_label.configure(image=img_enc_display)
self.image_label.image = img_enc_display
self.image_label['text'] = 'Encrypted Image:'
def display_encrypted_image(self):
if os.path.exists(self.encrypted_img_path):
img_enc = Image.open(self.encrypted_img_path)
img_enc.thumbnail((400, 400))
img_enc_display = ImageTk.PhotoImage(img_enc)
self.image_label.configure(image=img_enc_display)
self.image_label.image = img_enc_display
self.image_label['text'] = 'Encrypted Image:'
else:
messagebox.showerror("Error", "No encrypted image found!")
def de_fun(self):
if self.original_image is not None:
img_dec_display = ImageTk.PhotoImage(self.original_image)
self.image_label.configure(image=img_dec_display)
self.image_label.image = img_dec_display
self.image_label['text'] = 'Decrypted Image:'
def exit_to_login(self):
self.init_login_screen()
if __name__ == "__main__":
root = tk.Tk()
app = ImageEncryptorApp(root)
root.mainloop()
OUTPUTS: