Python使用pytesseract读取图像中的数字

1、在做登陆接口自动化时,可能会遇到需要验证码的情况,这时可以利用python识别图像的库来识别图中的验证码;

2、在获取验证码的接口中提取验证码的值

3、使用pytesseract来识别图片验证码,以下是一个示例代码:

import pytesseract
import base64
from PIL import Image
def convert_png(captcha_value)
    #以下的base64文件必须是图片转换的
    image = image_path.split(",")[1]        #只需要captcha_value中“base64”后面的
    img = base64.b64decode(image)           #将base64转换成图片
    with open('captcha.png','wb') as f:
        f.write(img)
    image = Image.open(r'captcha.png')
    text = pytesseract.image_to_string(image,lang='eng',config='--psm 10 --oem 3 -c tessedit_char_whitelist=0123456789')
    text = text.replace("\n\f","")        #0.3.10版本的pytesseract识别出来多了\n\f,用空格替换一下
    return text
convert_png(captcha_value)

4、识别出图片

相关推荐

  1. 使用python读取多类型文件夹文档内容】

    2024-01-18 14:34:01       14 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-18 14:34:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-18 14:34:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-18 14:34:01       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-18 14:34:01       20 阅读

热门阅读

  1. 智能小程序相关名词解释(汇总)

    2024-01-18 14:34:01       34 阅读
  2. uniapp返回上一页并刷新数据

    2024-01-18 14:34:01       32 阅读
  3. MySQL部署

    2024-01-18 14:34:01       28 阅读
  4. jupyter中使用conda环境

    2024-01-18 14:34:01       34 阅读
  5. 历史日志·洪武朝武功一览

    2024-01-18 14:34:01       28 阅读