目 录CONTENT

文章目录

python获取图片中的文字

懿曲折扇情
2023-07-18 / 0 评论 / 1 点赞 / 663 阅读 / 318 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2023-07-18,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。
广告 广告

一、背景

项目中使用中python识别图片中的文字,所以就有了下文

二、依赖环境

1.安装tesseract(我选择了最新的包)

安装包地址:
https://digi.bib.uni-mannheim.de/tesseract/

image-1689645664881

注意:记住安装路径后面会配置环境变量用到

2.安装中文语言包

安装包地址:
https://tesseract-ocr.github.io/tessdoc/Data-Files
image-1689645754571

3.配置环境变量

添加用户变量:TESSDATA_PREFIX

image-1689645817868

添加环境变量

image-1689645918077

4.测试是否安装成功

终端执行tesseract -v
image-1689645995942

5.执行代码

# coding=utf-8
"""
    @project: automation_tools
    @Author:gaojs
    @file: test043.py
    @date:2023/7/17 15:07
    @blogs: https://www.gaojs.com.cn
"""
import pytesseract
from PIL import Image


def get_text_from_photo(photo_path):
    """
    从图片中获取文字
    """
    # 读取图片
    im = Image.open(photo_path)
    # 识别文字,并指定语言
    text = pytesseract.image_to_string(im, lang='chi_sim')
    print(text)
    return text


if __name__ == '__main__':
    get_text_from_photo(photo_path='test043.png')
    

6.错误提示

pytesseract.pytesseract.TesseractNotFoundError: tesseract is not installed or it's not in your PATH. See README file for more information.

7.解决报错

在pytesseract库下的pytesseract.py文件中找到tesseract_cmd = 'tesseract',修改成 tesseract_cmd =r'D:\OCR\tesseract.exe'

image-1689646199506

8.成功运行

image-1689646381893

1

评论区