-
Notifications
You must be signed in to change notification settings - Fork 1
/
CaptchaTrainer.py
78 lines (69 loc) · 2.45 KB
/
CaptchaTrainer.py
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
import requests
import os
import PIL.Image
import PIL.ImageTk
import shutil
from tkinter import *
from tkinter import messagebox
from captcha import *
class CaptchaTrainer(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.decoder = CaptchaDecoder()
self.grid()
self.createWidgets()
self.getAndCheck()
def createWidgets(self):
self.img = PIL.ImageTk.PhotoImage(PIL.Image.open("pic.jpg"))
self.imageLabel = Label(self,image=self.img)
self.imageLabel.grid(row=0, column=0)
self.displayText = Label(self)
self.displayText["text"] = "something happened"
self.displayText.grid(row=1, column=0, columnspan=4)
self.imageField = Entry(self)
self.imageField["width"] = 10
self.imageField.grid(row=2, column=0, columnspan=1)
submit = Button(self)
submit["text"] = "Ok"
submit["command"] = self.submitResult
submit.grid(row=3,column=0)
def submitResult(self):
tmplist = os.listdir('tmp')
input = self.imageField.get()
if len(tmplist) == len(input):
for i in range(len(input)):
c = input[i]
filelist = os.listdir('templates/'+c)
if filelist:
last = filelist[len(filelist)-1].split('.')
num = int(last[0])+1
else:
num = 0
if num < 10:
num = '000'+str(num)
elif num < 100:
num = '00' + str(num)
elif num < 1000:
num = '0' + str(num)
filename = 'templates/'+c+'/'+num+'.png'
print(filename)
shutil.copy('tmp/'+tmplist[i],filename)
#else:
#messagebox.showinfo("Error","length not match")
for f in tmplist:
os.remove('tmp/'+f)
self.getAndCheck()
def getAndCheck(self):
image_url = "http://railway.hinet.net/ImageOut.jsp"
r = requests.get(image_url)
with open("img.jpg","wb") as f:
f.write(r.content)
str = self.decoder.identify("img.jpg")
self.displayText['text'] = str
self.img = PIL.ImageTk.PhotoImage(PIL.Image.open("img.jpg"))
self.imageLabel['image'] = self.img
if __name__ == '__main__':
root = Tk()
root.title('TicketBooker')
app = CaptchaTrainer(master=root)
app.mainloop()