Repository navigation
Expand file tree
/
Copy pathimgToCode.py
More file actions
73 lines (53 loc) · 1.66 KB
/
Copy pathimgToCode.py
File metadata and controls
73 lines (53 loc) · 1.66 KB
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
import os
import json
import base64
import time
import hashlib
class ConvertToCode:
__imgpath = ""
def genFilename(self, src):
des = src + str(time.time())
m2 = hashlib.md5()
m2.update(des)
return m2.hexdigest()
def webDataToimg(self, webdata):
jsondata = json.loads(webdata)
base64code = jsondata["base64"]
self.__imgpath = self.genFilename(base64code)
imgdata = base64.b64decode(base64code)
imgfile = open(self.__imgpath, 'wb')
imgfile.write(imgdata)
imgfile.close()
def rmFile(self, path):
if os.path.exists(path):
os.remove(path)
def imgToCode(self):
#self.__imgpath = "img.png"
codefile_path = self.__imgpath + ".txt"
self.rmFile(codefile_path)
if (not os.path.exists(self.__imgpath)):
return "picture not exists"
command = "tesseract " + self.__imgpath + " " + self.__imgpath + " -l eng"
os.system(command)
count = 0
wait_time = 3
while (not os.path.exists(codefile_path)):
time.sleep(1)
count += 1
if count == wait_time:
return "convert time too long"
resFile = open(codefile_path, "r")
res = resFile.read()
resFile.close()
self.rmFile(codefile_path)
self.rmFile(self.__imgpath)
return res
def webDataToCode(self, webdata):
self.webDataToimg(webdata)
res = self.imgToCode()
return res
def run():
test = ConvertToCode()
print test.genFilename("test")
print test.imgToCode()
#run()