1
+
2
+ # Image Encryption Decryption
3
+
4
+ # imported necessary library
5
+ import tkinter
6
+ from tkinter import *
7
+ import tkinter as tk
8
+ import tkinter .messagebox as mbox
9
+ from tkinter import ttk
10
+ from tkinter import filedialog
11
+ from PIL import ImageTk , Image
12
+ import cv2
13
+ import os
14
+ import numpy as np
15
+ from cv2 import *
16
+ import random
17
+
18
+ #created main window
19
+ window = Tk ()
20
+ window .geometry ("1000x700" )
21
+ window .title ("Image Encryption Decryption" )
22
+
23
+ # defined variable
24
+ global count , emig
25
+ # global bright, con
26
+ # global frp, tname # list of paths
27
+ frp = []
28
+ tname = []
29
+ con = 1
30
+ bright = 0
31
+ panelB = None
32
+ panelA = None
33
+
34
+ # function defined to get the path of the image selected
35
+ def getpath (path ):
36
+ a = path .split (r'/' )
37
+ # print(a)
38
+ fname = a [- 1 ]
39
+ l = len (fname )
40
+ location = path [:- l ]
41
+ return location
42
+
43
+ # function defined to get the folder name from which image is selected
44
+ def getfoldername (path ):
45
+ a = path .split (r'/' )
46
+ # print(a)
47
+ name = a [- 1 ]
48
+ return name
49
+
50
+ # function defined to get the file name of image is selected
51
+ def getfilename (path ):
52
+ a = path .split (r'/' )
53
+ fname = a [- 1 ]
54
+ a = fname .split ('.' )
55
+ a = a [0 ]
56
+ return a
57
+
58
+ # function defined to open the image file
59
+ def openfilename ():
60
+ filename = filedialog .askopenfilename (title = '"pen' )
61
+ return filename
62
+
63
+ # function defined to open the selected image
64
+ def open_img ():
65
+ global x , panelA , panelB
66
+ global count , eimg , location , filename
67
+ count = 0
68
+ x = openfilename ()
69
+ img = Image .open (x )
70
+ eimg = img
71
+ img = ImageTk .PhotoImage (img )
72
+ temp = x
73
+ location = getpath (temp )
74
+ filename = getfilename (temp )
75
+ # print(x)
76
+ if panelA is None or panelB is None :
77
+ panelA = Label (image = img )
78
+ panelA .image = img
79
+ panelA .pack (side = "left" , padx = 10 , pady = 10 )
80
+ panelB = Label (image = img )
81
+ panelB .image = img
82
+ panelB .pack (side = "right" , padx = 10 , pady = 10 )
83
+ else :
84
+ panelA .configure (image = img )
85
+ panelB .configure (image = img )
86
+ panelA .image = img
87
+ panelB .image = img
88
+
89
+ # function defined for make the sketch of image selected
90
+ def en_fun ():
91
+ global x , image_encrypted , key
92
+ # print(x)
93
+ image_input = imread (x , IMREAD_GRAYSCALE )# 'C:/Users/aakas/Documents/flower.jpg'
94
+ (x1 , y ) = image_input .shape
95
+ image_input = image_input .astype (float ) / 255.0
96
+ # print(image_input)
97
+
98
+ mu , sigma = 0 , 0.1 # mean and standard deviation
99
+ key = np .random .normal (mu , sigma , (x1 , y )) + np .finfo (float ).eps
100
+ # print(key)
101
+ image_encrypted = image_input / key
102
+ imwrite ('image_encrypted.jpg' , image_encrypted * 255 )
103
+
104
+ imge = Image .open ('image_encrypted.jpg' )
105
+ imge = ImageTk .PhotoImage (imge )
106
+ panelB .configure (image = imge )
107
+ panelB .image = imge
108
+ mbox .showinfo ("Encrypt Status" , "Image Encryted successfully." )
109
+
110
+ # function defined to make the image sharp
111
+ def de_fun ():
112
+ global image_encrypted , key
113
+ image_output = image_encrypted * key
114
+ image_output *= 255.0
115
+ imwrite ('image_output.jpg' , image_output )
116
+
117
+ imgd = Image .open ('image_output.jpg' )
118
+ imgd = ImageTk .PhotoImage (imgd )
119
+ panelB .configure (image = imgd )
120
+ panelB .image = imgd
121
+ mbox .showinfo ("Decrypt Status" , "Image decrypted successfully." )
122
+
123
+
124
+ # function defined to reset the edited image to original one
125
+ def reset ():
126
+ # print(x)
127
+ image = cv2 .imread (x )[:, :, ::- 1 ]
128
+ global count , eimg
129
+ count = 6
130
+ global o6
131
+ o6 = image
132
+ image = Image .fromarray (o6 )
133
+ eimg = image
134
+ image = ImageTk .PhotoImage (image )
135
+ panelB .configure (image = image )
136
+ panelB .image = image
137
+ mbox .showinfo ("Success" , "Image reset to original format!" )
138
+
139
+ # function defined to same the edited image
140
+ def save_img ():
141
+ global location , filename , eimg
142
+ print (filename )
143
+ # eimg.save(location + filename + r"_edit.png")
144
+ filename = filedialog .asksaveasfile (mode = 'w' , defaultextension = ".jpg" )
145
+ if not filename :
146
+ return
147
+ eimg .save (filename )
148
+ mbox .showinfo ("Success" , "Encrypted Image Saved Successfully!" )
149
+
150
+
151
+
152
+ # top label
153
+ start1 = tk .Label (text = "Image Encryption\n Decryption" , font = ("Arial" , 40 ), fg = "magenta" ) # same way bg
154
+ start1 .place (x = 350 , y = 10 )
155
+
156
+ # original image label
157
+ start1 = tk .Label (text = "Original\n Image" , font = ("Arial" , 40 ), fg = "magenta" ) # same way bg
158
+ start1 .place (x = 100 , y = 270 )
159
+
160
+ # edited image label
161
+ start1 = tk .Label (text = "Encrypted\n Decrypted\n Image" , font = ("Arial" , 40 ), fg = "magenta" ) # same way bg
162
+ start1 .place (x = 700 , y = 230 )
163
+
164
+ # choose button created
165
+ chooseb = Button (window , text = "Choose" ,command = open_img ,font = ("Arial" , 20 ), bg = "orange" , fg = "blue" , borderwidth = 3 , relief = "raised" )
166
+ chooseb .place (x = 30 , y = 20 )
167
+
168
+ # save button created
169
+ saveb = Button (window , text = "Save" ,command = save_img ,font = ("Arial" , 20 ), bg = "orange" , fg = "blue" , borderwidth = 3 , relief = "raised" )
170
+ saveb .place (x = 170 , y = 20 )
171
+
172
+ # Encrypt button created
173
+ enb = Button (window , text = "Encrypt" ,command = en_fun ,font = ("Arial" , 20 ), bg = "light green" , fg = "blue" , borderwidth = 3 , relief = "raised" )
174
+ enb .place (x = 150 , y = 620 )
175
+
176
+ # decrypt button created
177
+ deb = Button (window , text = "Decrypt" ,command = de_fun ,font = ("Arial" , 20 ), bg = "orange" , fg = "blue" , borderwidth = 3 , relief = "raised" )
178
+ deb .place (x = 450 , y = 620 )
179
+
180
+ # reset button created
181
+ resetb = Button (window , text = "Reset" ,command = reset ,font = ("Arial" , 20 ), bg = "yellow" , fg = "blue" , borderwidth = 3 , relief = "raised" )
182
+ resetb .place (x = 800 , y = 620 )
183
+
184
+ # function created for exiting
185
+ def exit_win ():
186
+ if mbox .askokcancel ("Exit" , "Do you want to exit?" ):
187
+ window .destroy ()
188
+
189
+ # exit button created
190
+ exitb = Button (window , text = "EXIT" ,command = exit_win ,font = ("Arial" , 20 ), bg = "red" , fg = "blue" , borderwidth = 3 , relief = "raised" )
191
+ exitb .place (x = 880 , y = 20 )
192
+
193
+
194
+ window .protocol ("WM_DELETE_WINDOW" , exit_win )
195
+ window .mainloop ()
0 commit comments