1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
import os import random from io import BytesIO import tkinter.messagebox from tkinter import * import imageio from PIL import ImageDraw from PIL import ImageFont from PIL import Image, ImageTk
class LabelDisplay(Label): def __init__(self, master, filename): image = Image.open(filename) sequence = [] try: while 1: sequence.append(image.copy()) image.seek(len(sequence)) except EOFError: pass
try: self.delay = image.info['duration'] except KeyError: self.delay = 100
begin = sequence[0].convert('RGBA') self.frames = [ImageTk.PhotoImage(begin)]
Label.__init__(self, master, image=self.frames[0])
frameTemp = sequence[0] for image in sequence[1:]: frameTemp.paste(image) frame = frameTemp.convert('RGBA') self.frames.append(ImageTk.PhotoImage(frame))
self.frameID = 0
self.cancel = self.after(self.delay, self.play)
def play(self): self.config(image=self.frames[self.frameID]) self.frameID += 1 if self.frameID == len(self.frames): self.frameID = 0 self.cancel = self.after(self.delay, self.play)
class DynamicVerificationCode(object): def __init__(self, breadth=200, vertical=55, strcount=5, fontsize=40, arccount=20, linecount=3, number_of_frames=30): self.breadth = breadth self.vertical = vertical self.strcount = strcount self.fontsize = fontsize self.arccount = arccount self.linecount = linecount self.number_of_frames = number_of_frames
def Colorconfusion(self): colorr = random.randint(0, 255) colorg = random.randint(0, 255) colorb = random.randint(0, 255) return colorr, colorg, colorb
def StrInDVC(self): number = str(random.randint(0, 9)) xiaoxieap = chr(random.randint(97, 122)) daxieap = chr(random.randint(65, 90)) reStr = random.choice([number, xiaoxieap, daxieap]) return reStr
def LineConfusion(self, createpic=None): for i in range(self.linecount): sourcex = random.randint(0, self.breadth) destx = random.randint(0, self.breadth) sourcey = random.randint(0, self.vertical) desty = random.randint(0, self.vertical) createpic.line((sourcex, sourcey, destx, desty), fill=self.Colorconfusion())
def ArcConfusion(self, createpic=None): for i in range(self.arccount): createpic.point([random.randint(0, self.breadth), random.randint(0, self.vertical)], fill=self.Colorconfusion()) x = random.randint(0, self.breadth) y = random.randint(0, self.vertical) createpic.arc((x, y, x + 4, y + 4), 0, 90, fill=self.Colorconfusion())
def Create(self): StrInDVCList = [] for i in range(self.strcount): s = self.StrInDVC() StrInDVCList.append(s)
background = self.Colorconfusion()
picSequence = [] for i in range(self.number_of_frames): DVC = Image.new('RGB', (self.breadth, self.vertical), background) createpic = ImageDraw.Draw(DVC)
fontInDVC = os.path.join(os.getcwd(), "COOPBL.TTF") font = ImageFont.truetype(fontInDVC, size=self.fontsize)
for i, code in enumerate(StrInDVCList): y = random.randint(int(-0.133 * self.vertical), int(0.267 * self.vertical)) x = random.randint(14, 22) createpic.text((x + i * (self.breadth / self.strcount), y), code, self.Colorconfusion(), font=font)
self.LineConfusion(createpic)
self.ArcConfusion(createpic)
PNGfile = BytesIO() DVC.save(PNGfile, "png") data = PNGfile.getvalue() PNGfile.close()
data = imageio.imread(data, format="png") picSequence.append(data)
return picSequence, "".join(StrInDVCList)
class GUI: def __init__(self): self.ans = self.DVCCreate() self.root = Tk() self.root.geometry('500x400+500+300') self.root.resizable(False, False) self.root.title("Dynamic Verification Code") self.anim = LabelDisplay(self.root, 'Dynamic_verification_code.gif') self.anim.pack(side="top") self.entryCode = tkinter.Entry(self.root) self.entryCode.place(x=150, y=200, width=200, height=30) self.w = Label(self.root, text="Please enter the verification code below:") self.w.place(x=121, y=168) self.ww = Label(self.root, text="Cybersecurity Coursework Build Date: December 8, 2021", font=('Consolas', 8), fg='DarkRed') self.ww.place(x=135, y=382) self.www = Label(self.root, text="You can refer to the comments to adjust the parameters of the DynamicVerificationCode(" ") function to adjust the style of the dynamic verification code.", wraplength=244, font=('Consolas', 10)) self.www.place(x=135, y=255)
self.btnCheck = tkinter.Button(self.root, text='Submit', width=20, height=2, command=self.check) self.btnCheck.pack(side='bottom', pady=20)
self.btnRe = tkinter.Button(self.root, text='Refresh', width=20, height=2, command=self.Re) self.btnRe.place(x=158, y=75) self.root.mainloop() self.Re()
def DVCCreate(self): img = DynamicVerificationCode(200, 55, 5, 40, 20, 3, 30) seq, ans = img.Create() imageio.mimsave("Dynamic_verification_code.gif", seq, 'GIF', duration=0.3) print("The verification code characters are " + ans) return ans
def check(self): if self.entryCode.get() == "": tkinter.messagebox.showerror('DVC Fatal Error', 'The verification code is empty!') else: if self.entryCode.get() == self.ans: tkinter.messagebox.showwarning('DVC Success', 'Successfully verified!') else: tkinter.messagebox.showerror('DVC Fail', 'Verification code error! Please try again.')
def Re(self): print("Re") self.ans = self.DVCCreate() self.anim.pack_forget() self.anim = LabelDisplay(self.root, 'Dynamic_verification_code.gif') self.anim.pack(side="top") self.root.update()
if __name__ == "__main__": GUI()
|