爬虫 - 图形验证码

用OCR来识别
直接识别效果不好,因为验证码内的多余线条干扰了图片的识别。先转为灰度图像,再二值化。经实践证明,该方法不是100%正确。

# 获取图片
curl -X GET http://my.cnki.net/elibregister/CheckCode.aspx

import tesserocr
from PIL import Image

image = Image.open('1.png')
# 转为灰度图像
image = image.convert('L')

threshold = 127
table = []

# 二值化
for i in range(256):
    if i < threshold:
        table.append(0)
    else:
        table.append(1)
# mode='1'默认的阀值为127
image = image.point(table, '1')
image.show()
result = tesserocr.image_to_text(image)
print(result)


posted @ 2018-08-26 15:00  Rocin  阅读(272)  评论(0编辑  收藏  举报