python实现OCR

python实现OCR

在Python中实现OCR(光学字符识别)通常需要使用第三方库,如pytesseract。以下是使用pytesseract进行OCR的基本步骤:

  1. 安装pytesseract和相关的OCR库,如tesseract-ocr

  2. 使用pytesseract库的image_to_string函数来识别图片中的文本。

首先,你需要安装pytesseracttesseract-ocr

pip install pytesseract

对于tesseract-ocr,你可以通过以下链接下载对应的安装程序:https://github.com/tesseract-ocr/tesseract

安装完成后,你可以使用以下代码进行OCR:

import pytesseract

from PIL import Image

设置tesseract-ocr安装路径

pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe' # 请替换为你的安装路径

打开图片

image = Image.open('example.png')

使用OCR识别图片中的文本

text = pytesseract.image_to_string(image, lang='eng')

print(text)

请确保替换example.png为你想要识别的图片文件名,并且根据你的安装路径设置tesseract_cmdlang='eng'指定OCR使用的语言,你可以根据需要识别的文本语言进行相应的设置。

相关推荐
alphaTao15 小时前
LeetCode 每日一题 2026/7/27-2026/8/2
python·算法·leetcode
Kevin Wang72715 小时前
Nvidia-AGX-spark部署手册——课堂质量诊断(jetpack:r36)
python·docker·容器
Python私教16 小时前
Django 6.1 RC1 实测:FETCH_PEERS 两条 SQL 解决 N+1,select_related 还需要吗?
后端·python·django
玉鸯16 小时前
Agent 的任务编排:从 System Prompt 到 Hierarchical Multi-Agent
python·llm·agent
Python私教16 小时前
Django 6.0 自带 Tasks 到底能不能替代 Celery?跑完 3 组后台任务后我有答案了
后端·python·django
我的xiaodoujiao16 小时前
快速学习Python基础知识详细图文教程14--模块
开发语言·python·学习·测试工具
残影飞雪16 小时前
Ollama对话脚本
python
jerryinwuhan17 小时前
数据预处理技术 2026-2027-1 开篇-课程介绍
大数据·python
看昭奚恤哭17 小时前
ontainer App】Container App无法从Container Registries 拉取镜像 - 报错 Forbidden
后端·python·flask
05664617 小时前
Python康复训练——数据结构
数据结构·windows·python