-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRecognize.py
53 lines (47 loc) · 1.38 KB
/
Recognize.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
__author__ = 'halfcrazy'
from PIL import Image
import StringIO
def binary(data):
data.seek(0)
img = Image.open(data)
#img = img.convert('1')
pixdata = img.load()
for y in xrange(img.size[1]):
for x in xrange(img.size[0]):
if pixdata[x, y][0] < 90:
pixdata[x, y] = (0, 0, 0, 255)
for y in xrange(img.size[1]):
for x in xrange(img.size[0]):
if pixdata[x, y][1] < 136:
pixdata[x, y] = (0, 0, 0, 255)
for y in xrange(img.size[1]):
for x in xrange(img.size[0]):
if pixdata[x, y][2] > 0:
pixdata[x, y] = (255, 255, 255, 255)
return img
def division(img):
font = []
for i in range(4):
x = 6 + i * 13
y = 3
font.append(img.crop((x, y, x + 9, y + 13)))
return font
def recognize(img):
fontMods = []
for i in range(10):
fontMods.append((str(i), Image.open('./zimo/%d.jpg' % i)))
result = ''
font = division(img)
for i in font:
target = i
points = []
for mod in fontMods:
diffs = 0
for yi in range(13):
for xi in range(9):
if mod[1].getpixel((xi, yi)) != target.getpixel((xi, yi)):
diffs += 1
points.append((diffs, mod[0]))
points.sort()
result += points[0][1]
return result