|
| 1 | +from tkinter import * |
| 2 | +import os |
| 3 | +import random |
| 4 | + |
| 5 | +#Creating GUI |
| 6 | +#welcome note |
| 7 | + |
| 8 | + |
| 9 | +root=Tk() |
| 10 | +root.title("EncRYPTO") |
| 11 | +Label(root,font=('arial',20,'bold'),text="WELCOME TO EncRYPTO").grid(columnspan=2) |
| 12 | +Label(root,font=('arial',10),text="Choose an image file to encrypt data into").grid(columnspan=2) |
| 13 | + |
| 14 | + |
| 15 | +#opens dialog to choose image file for encryption |
| 16 | + |
| 17 | + |
| 18 | +from tkinter.filedialog import askopenfilename |
| 19 | +filename = askopenfilename() |
| 20 | + |
| 21 | + |
| 22 | +#Accessing image and image matrix |
| 23 | + |
| 24 | +from PIL import Image |
| 25 | +im=Image.open(filename).convert('L') |
| 26 | +data=im.load() |
| 27 | +w,h=im.size |
| 28 | +img=Image.open(filename).convert('L') |
| 29 | +final=img.load() |
| 30 | +l=[] |
| 31 | +central=[] |
| 32 | +c=0 |
| 33 | + |
| 34 | +#Creating pixel intensity matrix where each row contains 5 columns which have all possible pixel values of an image(5 values in 1 row). |
| 35 | + |
| 36 | +for i in range(0,255,5): |
| 37 | + t=[x for x in range(i,i+5)] |
| 38 | + l.append(t) |
| 39 | + central.append(i+2) |
| 40 | + |
| 41 | +#Pixel Mapping Reference to encrypt Image |
| 42 | + |
| 43 | +pm=(100,101,110,111) |
| 44 | + |
| 45 | +#Creating Location Map |
| 46 | + |
| 47 | +loc_map="" |
| 48 | +for i in range(im.size[0]): |
| 49 | + for j in range(im.size[1]): |
| 50 | + k=0 |
| 51 | + for v in l: |
| 52 | + if(v[2]==data[i,j]): |
| 53 | + loc_map=loc_map+"1" |
| 54 | + k=1 |
| 55 | + break |
| 56 | + if(k==0): |
| 57 | + loc_map=loc_map+"0" |
| 58 | + |
| 59 | +#Generating Random data to hide into image |
| 60 | + |
| 61 | +from random import randint |
| 62 | +d="" |
| 63 | +for i in range(5120): |
| 64 | + d=d+str(randint(0,1)) |
| 65 | + |
| 66 | +#Embedding data into image using a reversible data hiding technique |
| 67 | + |
| 68 | +di=0 |
| 69 | +flag=0 |
| 70 | +c=0 |
| 71 | +for i in range(w): |
| 72 | + for j in range(h): |
| 73 | + if(final[i,j] in central): |
| 74 | + while(c<len(d)): |
| 75 | + if(d[c]=='1'): |
| 76 | + y=d[c:c+3] |
| 77 | + ind=central.index(final[i,j]) |
| 78 | + tup=l[ind] |
| 79 | + if(int(y)==pm[0]): |
| 80 | + final[i,j]=tup[0] |
| 81 | + elif(int(y)==pm[1]): |
| 82 | + final[i,j]=tup[1] |
| 83 | + elif(int(y)==pm[2]): |
| 84 | + final[i,j]=tup[3] |
| 85 | + elif(int(y)==pm[3]): |
| 86 | + final[i,j]=tup[4] |
| 87 | + break |
| 88 | + else: |
| 89 | + pass |
| 90 | + c=c+1 |
| 91 | + |
| 92 | +#Calculating PSNR value of the encrypted image |
| 93 | + |
| 94 | +import math |
| 95 | + |
| 96 | +s=0 |
| 97 | +for i in range(w): |
| 98 | + for j in range(h): |
| 99 | + s=s+(data[i,j]-final[i,j])**2 |
| 100 | +s=s/(w*h) |
| 101 | +PSNR=10*math.log10((255*255)/s) |
| 102 | + |
| 103 | +#Saving the encrypted image |
| 104 | + |
| 105 | +img.save("Encrypted.png") |
| 106 | + |
| 107 | +#Creating GUI to display results |
| 108 | + |
| 109 | +Label(root,font=('arial',20,'bold'),text="IMAGE ENCRYPTED").grid(columnspan=2) |
| 110 | +Label(root,text="MSE value is :"+str(s)).grid(columnspan=2) |
| 111 | +Label(root,text="PSNR value is :"+str(PSNR)).grid(columnspan=2) |
| 112 | + |
| 113 | + |
| 114 | +def display_Encrypted(): |
| 115 | + os.system("Encrypted.png") |
| 116 | +def display(): |
| 117 | + os.system(filename) |
| 118 | +def exit(): |
| 119 | + root.destroy() |
| 120 | +Button(root, text='Display Original Image', command=display).grid(row=5,column=0) |
| 121 | +Button(root, text='Display Encrypted Image', command=display_Encrypted).grid(row=5,column=1,sticky=W) |
| 122 | +Button(root, text='Exit',command=exit,width=10).grid(row=7,columnspan=2) |
| 123 | + |
| 124 | +#STATUS OF OUR PROJECT |
| 125 | + |
| 126 | +"""The project stands completed. |
| 127 | +We have made use of the Python Imaging Library to access and process the image |
| 128 | +and hide data into it. We have also used the tkinter library to make a graphical |
| 129 | +user interface or GUI to make the program user friendly. The code makes use of pixel |
| 130 | +intensity segmentation.In this technique, we have divided all possible pixels of image |
| 131 | +into groups of five out of which the central pixel value is the concealable pixel. Then |
| 132 | +the loop goes through the matrix object and finds all the concealable pixels and hides |
| 133 | +data accordingly.""" |
| 134 | + |
| 135 | + |
| 136 | + |
| 137 | + |
| 138 | + |
| 139 | + |
| 140 | + |
0 commit comments