Python 实现简单OCR文本识别

Ubuntu系统:22.04

python版本:3.9

安装依赖库:

bash 复制代码
# 安装Tesseract引擎和开发库
sudo apt update && sudo apt install tesseract-ocr libtesseract-dev

# 安装英语+中文语言包
sudo apt install tesseract-ocr-eng tesseract-ocr-chi-sim

# 安装Python依赖
pip install pytesseract pillow -i https://mirrors.aliyun.com/pypi/simple

代码实现:

python 复制代码
# 安装必要依赖(Ubuntu/Debian)
# 先执行以下终端命令:
# sudo apt update && sudo apt install tesseract-ocr libtesseract-dev
# sudo apt install tesseract-ocr-chi-sim  # 中文支持(可选)
# pip3 install pytesseract pillow

from PIL import Image
import pytesseract
import sys
import os

def ocr_core(image_path):
    """
    核心OCR函数
    :param image_path: 图片路径
    :return: 识别后的文本
    """
    try:
        if not os.path.exists(image_path):
            raise FileNotFoundError(f"文件 {image_path} 不存在")

        img = Image.open(image_path)
        
        # 多语言识别示例(英语+中文)
        text = pytesseract.image_to_string(img, lang='eng+chi_sim')
        
        return text.strip() if text else "未识别到文字"
    
    except Exception as e:
        return f"错误: {str(e)}"

if __name__ == "__main__":
    if len(sys.argv) > 1:
        image_path = sys.argv[1]
    else:
        image_path = input("请输入图片路径:").strip()
    
    print("\n识别中...")
    result = ocr_core(image_path)
    
    print("\n识别结果:")
    print("-" * 30)
    print(result)
    print("-" * 30)

下载测试图片:

bash 复制代码
# 下载测试图片(可选)
wget https://tesseract.projectnaptha.com/img/eng_bw.png -O test.png

# 执行识别
python ocr_demo.py test.png
相关推荐
牛奔6 小时前
Go 如何避免频繁抢占?
开发语言·后端·golang
寻星探路10 小时前
【深度长文】万字攻克网络原理:从 HTTP 报文解构到 HTTPS 终极加密逻辑
java·开发语言·网络·python·http·ai·https
lly20240612 小时前
Bootstrap 警告框
开发语言
2601_9491465312 小时前
C语言语音通知接口接入教程:如何使用C语言直接调用语音预警API
c语言·开发语言
曹牧12 小时前
Spring Boot:如何测试Java Controller中的POST请求?
java·开发语言
KYGALYX12 小时前
服务异步通信
开发语言·后端·微服务·ruby
zmzb010313 小时前
C++课后习题训练记录Day98
开发语言·c++
ValhallaCoder13 小时前
hot100-二叉树I
数据结构·python·算法·二叉树
猫头虎13 小时前
如何排查并解决项目启动时报错Error encountered while processing: java.io.IOException: closed 的问题
java·开发语言·jvm·spring boot·python·开源·maven
YUJIANYUE14 小时前
PHP纹路验证码
开发语言·php