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

相关推荐
用户8356290780516 小时前
无需 Office:Python 批量转换 PPT 为图片
后端·python
markfeng88 小时前
Python+Django+H5+MySQL项目搭建
python·django
GinoWi9 小时前
Chapter 2 - Python中的变量和简单的数据类型
python
JordanHaidee9 小时前
Python 中 `if x:` 到底在判断什么?
后端·python
ServBay9 小时前
10分钟彻底终结冗长代码,Python f-string 让你重获编程自由
后端·python
闲云一鹤10 小时前
Python 入门(二)- 使用 FastAPI 快速生成后端 API 接口
python·fastapi
Rockbean11 小时前
用40行代码搭建自己的无服务器OCR
服务器·python·deepseek
曲幽11 小时前
FastAPI + Ollama 实战:搭一个能查天气的AI助手
python·ai·lora·torch·fastapi·web·model·ollama·weatherapi
用户606487671889612 小时前
国内开发者如何接入 Claude API?中转站方案实战指南(Python/Node.js 完整示例)
人工智能·python·api
只与明月听13 小时前
RAG深入学习之Chunk
前端·人工智能·python