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

相关推荐
geovindu14 分钟前
python: Face Recognition
开发语言·后端·python·人脸识别
2601_9548118228 分钟前
AI科学实验室MHS标准解读:AI智能体如何统一控制实验室设备接口
人工智能·python
一位正在转型AI全栈的前端工程师28 分钟前
AI 全栈学习之旅 -Week 9:什么是AI Agent?从Function Calling到LangGraph
前端·python
用户03321266636730 分钟前
使用 Python 为 PowerPoint 设置背景色和背景图【附代码示例】
python
ever_up9731 小时前
LangChain基础知识概述1
人工智能·python·langchain
asdzx671 小时前
Python 实现 PDF 文本查找与高亮标注
开发语言·python·pdf
zander2581 小时前
LeetCode 1143. 最长公共子序列
开发语言·python·算法
轩情吖2 小时前
Python基础知识点完整总结
python·字符串·编程语言·函数·列表·元组·字典
青 春 记 忆2 小时前
零基础入门python69:为 FastAPI 项目构建可复现 Docker 镜像
python·fastapi·后端开发
<花开花落>2 小时前
Python 项目迁移到 uv:经验小结与可复用工作流
python·uv