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使用的语言,你可以根据需要识别的文本语言进行相应的设置。

相关推荐
0566461 天前
agent学习Day15——SQLAlchemy 查询过滤、分页与历史列表接口
python·学习·fastapi
程序员爱德华1 天前
Python与C++:异同点对比
c++·python
hangyuekejiGEO1 天前
GEO技术服务选型指南
大数据·人工智能·python
软萌萌的11 天前
Java Spring Boot 修改yml配置&加载顺序规则
java·spring boot·python
Dxy12393102161 天前
Linux 编译安装 Python 3.12.10(多版本共存,不破坏系统Python)
linux·运维·python
持敬chijing1 天前
Python概述
开发语言·python
赤羽尾风1 天前
NumPy快速入门
python·numpy
倒流时光三十年1 天前
第三阶段 26 · highlight 高亮(返回命中片段)
后端·python·django
久久学姐1 天前
Python+Playwright+Pytest+BDD,用FSM打造高效测试框架
python·pytest·bdd·playwright·fsm