-
Notifications
You must be signed in to change notification settings - Fork 20
/
PicProcess.py
88 lines (73 loc) · 1.76 KB
/
PicProcess.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
79
80
81
82
83
84
85
86
87
88
from PIL import Image
from StringIO import StringIO
#Constances
BACKCOLOR = 255
TEXTCOLOR = 0
OriImgNum1_Left = 7
OriImgNum1_Upper = 3
OriImgNum1_Right = 16
OriImgNum1_Lower = 16
OriImgNums_Interval = 13
ModelWidth = 9
ModelHeight = 13
Deviation = 50
N = 100000
def Binarized(Picture):
imgB = Picture.copy()
Pixels = Picture.load();
PixelsB = imgB.load();
(Width, Height) = Picture.size
Threshold = 140
for i in xrange(Height):
for j in xrange(Width):
if Pixels[j, i] > Threshold:
PixelsB[j, i] = BACKCOLOR
else:
PixelsB[j, i] = TEXTCOLOR
return imgB
def getResultFromImage(img):
imgL = img.convert('L')
imgB = Binarized(imgL)
nums = getNums(imgB)
return nums
def cutNumsFromBimg(infilename):
left = OriImgNum1_Left
upper = OriImgNum1_Upper
right = OriImgNum1_Right
lower = OriImgNum1_Lower
interval = OriImgNums_Interval
img = Image.open(infilename)
for i in range(0, 4):
box = (left, upper, right, lower)
region = img.crop(box)
region.save('%s%s%d%s' % ('NUMS/', infilename[-5], i, '.jpg'))
left += interval
right += interval
def getNums(ImgB):
left = OriImgNum1_Left
upper = OriImgNum1_Upper
PixelsB = ImgB.load()
nums = ''
num = 0
for i in range(0, 4):
max = 0
for j in range(0, 10):
similarity = 0
imgModel = Image.open('%s%i%s'%('Models/', j, '.jpg'))
PixelsM = imgModel.load()
for h in xrange(ModelHeight):
for w in xrange(ModelWidth):
if abs(PixelsM[w, h] - PixelsB[left+w, upper+h]) < Deviation:
similarity+=1
else:
similarity-=1
if similarity>max:
max = similarity
num = j
left += OriImgNums_Interval
nums = '%s%d' % (nums, num)
return nums
def getResutlFromStr(buffer):
img = Image.open(StringIO(buffer))
nums = getResultFromImage(img)
return nums