Tesseract-OCR识别图片验证码

Tesseract的安装

windows安装包:https://digi.bib.uni-mannheim.de/tesseract/,最后一个是最新的。

默认安装路径 C:\Program Files (x86)\Tesseract-OCR, 将其添加到系统环境变量Path。

安装完成之后,在命令行输入 tesseract -v 测试是否安装成功。

识别图片:

tesseract 图片路径 输出文件

识别

1. 直接识别

import pytesseract
from PIL import Image


captcha = Image.open("b.jpg")
result = pytesseract.image_to_string(captcha)
print(result)   #6067

2.二值化处理

二值化处理前必须先转成灰度图,然后再设定一个阈值。

import pytesseract
from PIL import Image


def convert_img(img, threshold):
    img = img.convert("L")  # 处理灰度
    pixels = img.load()
    for x in range(img.width):
        for y in range(img.height):
            if pixels[x, y] > threshold:
                pixels[x, y] = 255
            else:
                pixels[x, y] = 0
    return img


captcha = Image.open("e.jpg")
captcha = convert_img(captcha, 150)
captcha.show()
captcha.save("threshold.jpg")
result = pytesseract.image_to_string(captcha)
print(result)  #3n3D

 

 

参考链接:

1. Windows安装Tesseract-OCR 4.00并配置环境变量

2. 小帅b教你如何用python识别图片验证码

posted @ 2020-01-17 16:20  Rogn  阅读(1824)  评论(0编辑  收藏  举报