-
Notifications
You must be signed in to change notification settings - Fork 0
/
yanzhenma.py
50 lines (44 loc) · 1.2 KB
/
yanzhenma.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import time
time1 = time.time()
from PIL import Image
import pytesseract
###########二值化算法
def binarizing(img,threshold):
pixdata = img.load()
w, h = img.size
for y in range(h):
for x in range(w):
if pixdata[x, y] < threshold:
pixdata[x, y] = 0
else:
pixdata[x, y] = 255
return img
image = Image.open(r'a.png')
###########去除干扰线算法
def depoint(img): #input: gray image
pixdata = img.load()
w,h = img.size
for y in range(1,h-1):
for x in range(1,w-1):
count = 0
if pixdata[x,y-1] > 245:
count = count + 1
if pixdata[x,y+1] > 245:
count = count + 1
if pixdata[x-1,y] > 245:
count = count + 1
if pixdata[x+1,y] > 245:
count = count + 1
if count > 2:
pixdata[x,y] = 255
return img
# 转化为灰度图
img = image.convert('L')
# 把图片变成二值图像。
img1=binarizing(img,190)
# img2=depoint(img1)
img1.show()
code = pytesseract.image_to_string(img1)
print ("识别该验证码是:" + str(code))