Skip to content

Commit 9606ecd

Browse files
authored
Merge pull request #141 from Arjun009/master
Added program to convert any number of any base to the required base …
2 parents 9d197dc + 021a537 commit 9606ecd

File tree

7 files changed

+285
-0
lines changed

7 files changed

+285
-0
lines changed
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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+
Loading
Loading
Loading
Loading
Loading
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
import datetime
2+
print("Today's Date and time is :", end=" ")
3+
print(str(datetime.datetime.now()))
4+
print("")
5+
print("------This program converts number with any base to the base entered by the User with guaranteed accuracy of minimum 3 decimal places.------")
6+
print(" Note: base should be less than or equal to 32 and greater than equal to 2 ")
7+
print("")
8+
print("**It rounds off the number upto 20th decimal places**")
9+
print("")
10+
tsssy=""
11+
while(tsssy==""):
12+
n=int(input("Enter base for the number"))
13+
print("")
14+
dic={'a':10,'b':11,'c':12,'d':13,'e':14,'f':15,'g':16,'h':17,'i':18,'j':19,'k':20,'l':21,'m':22,'n':23,'o':24,'p':25,'q':26,'r':27,'s':28,'t':29,'u':30,'v':31}
15+
rttu=""
16+
if n>10:
17+
xyzA=12
18+
while(xyzA>0):
19+
asd=[]
20+
akd=[]
21+
m=input("Enter Number with base "+str(n)).lower()
22+
print("")
23+
gg="".join(m)
24+
gg=gg.split(".")
25+
m=gg[0]
26+
xyz=0
27+
tty=0
28+
ks=0
29+
if len(gg)==2:
30+
for kjj in range(len(gg[1])):
31+
if 97<=ord(gg[1][kjj])<=122:
32+
u=gg[1][kjj]
33+
ks=dic[u]
34+
if int(ks)>=n:
35+
xyz=-1
36+
else:
37+
akd.append(str(ks))
38+
else:
39+
akd.append(gg[1][kjj])
40+
41+
42+
43+
for i in range(len(m)):
44+
if 97<=ord(m[i])<=122:
45+
u=m[i]
46+
ks=dic[u]
47+
if int(ks)>=n:
48+
xyz=-1
49+
else:
50+
asd.append(str(ks))
51+
else:
52+
asd.append(m[i])
53+
if xyz==-1:
54+
xyzA=12
55+
print("Enter number again correctly, Make sure digits for the number should be less than the base of the number")
56+
else:
57+
xyzA=-9
58+
59+
if n<=10:
60+
xyzA=12
61+
while(xyzA>0):
62+
asd=[]
63+
akd=[]
64+
m=input("Enter Number with base "+str(n)).lower()
65+
print("")
66+
gg="".join(m)
67+
gg=gg.split(".")
68+
m=gg[0]
69+
xyz=0
70+
ks=0
71+
if len(gg)==2:
72+
for kjj in range(len(gg[1])):
73+
ks=gg[1][kjj]
74+
if int(ks)>=n:
75+
xyz=-1
76+
77+
else:
78+
akd.append(gg[1][kjj])
79+
for i in range(len(m)):
80+
if int(m[i])>=n:
81+
xyz=-1
82+
else:
83+
asd.append(m[i])
84+
if xyz==-1:
85+
xyzA=12
86+
print("Enter number again correctly, Make sure digits for the number should be less than the base of the number")
87+
else:
88+
xyzA=-9
89+
lm=int(input("Enter base to which you want to convert the given number"))
90+
print("")
91+
c=0
92+
d=0
93+
e=-1
94+
f=0
95+
t=1
96+
y=""
97+
if len(gg)==2:
98+
hj=1
99+
uu=1
100+
101+
for ty in range(len(akd)):
102+
f+=int(akd[ty])*(n**e)
103+
e-=1
104+
105+
while(hj<=20):
106+
uu=f*lm
107+
108+
109+
if int(uu)>=10:
110+
for i in range(97,119):
111+
if dic[chr(i)]==int(uu):
112+
rttu+=chr(i)
113+
if int(uu)<10:
114+
rttu+=str(int(uu))
115+
f=uu-int(uu)
116+
hj+=1
117+
118+
119+
for i in range(len(asd)-1,-1,-1):
120+
c+=int(asd[i])*(n**d)
121+
d+=1
122+
123+
while(t>0):
124+
ggg=c%lm
125+
if int(ggg)>=10:
126+
for iu in range(97,119):
127+
if dic[chr(iu)]==int(ggg):
128+
y+=chr(iu)
129+
if int(ggg)<10:
130+
y+=str(c%lm)
131+
c=c//lm
132+
if c==0:
133+
t=-1
134+
print("Number with base "+str(n)+" was "+".".join(gg)+" .Now it is converted into base "+str(lm)+" the required number is:",end=" ")
135+
print(y[::-1],end=".")
136+
if len(rttu)!=0:
137+
138+
print(rttu)
139+
if len(rttu)==0:
140+
print(0)
141+
print("")
142+
uy=input("Press Enter to continue or hit any key to Exit !")
143+
144+
145+

0 commit comments

Comments
 (0)