关于使用tesserocr识别图片验证码

# coding:utf-8
import json
import requests
from io import BytesIO
import base64
# 请求头
import tesserocr
from PIL import Image

headers = {
    'Connection': 'keep-alive',
    'Accept': '*/*',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36',
    'Origin': 'http://www.shenghui56.com',
    'Referer': 'http://www.shenghui56.com/',
    'Accept-Language': 'zh-CN,zh;q=0.9',
}


# 获取验证码
def query_key():
    r = requests.get('****************', headers=headers,
                     verify=False)
    data = json.loads(r.content).get("obj")
    url = data.get("image")
    # 在线获取图片对象
    image_data = base64.b64decode(url)  # 获取解码后的bytes
    bytes_io = BytesIO()
    bytes_io.write(image_data)  # 写入bytes,把它当作文件读取
    img = Image.open(bytes_io)  # 读取'图片'文件
    img = img.convert("L")  # 转为灰度图像
    # 二值处理
    threshold = 100  # 设置二值的阈值100
    table = []
    for i in range(256):
        if i < threshold:
            table.append(0)
        else:
            table.append(1)
    img = img.point(table, "1")  # 返回给定查找表对应的图像像素值的拷贝,变量table为图像的每个通道设置256个值,
    # 为输出图像指定一个新的模式,模式为“L”和“P”的图像进一步转换为模式为“1”的图像
    # 解析验证码
    key = tesserocr.image_to_text(img)
    # 返回key
    return key, data


# 获取信息
def query_data(no, key, data):
    print(key.strip())
    params = (
        ('orderid', no),
        ('verityCodeKey', key),
        ('identifier', data.get("identifier")),
    )
    r = requests.get('*****************', headers=headers,
                     params=params, verify=False)
    return r.text


if __name__ == '__main__':
    the_key, data = query_key()
    a = query_data("285686d57a7185788", the_key, data)
    print(a)
原文地址:https://www.cnblogs.com/fwjlucifinil/p/13345863.html