python实现OCR

python实现OCR

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

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

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

首先,你需要安装pytesseract和tesseract-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_cmd。lang='eng'指定OCR使用的语言,你可以根据需要识别的文本语言进行相应的设置。

相关推荐
小小张说故事4 分钟前
pandas 读大 CSV 太慢?6 个实测提速技巧(dtype / 分块 / 引擎选择)
后端·python·pandas
yi0118 分钟前
DAY17: LeetCode 139|单词拆分:从“把单词删掉”到用 DP 记录合法切口
人工智能·笔记·python·算法·leetcode·动态规划
Java的搬运工12 分钟前
【无标题】
python·机器学习·docker·fastapi·模型部署·mlops·ai工程化
小小张说故事32 分钟前
SHAP 可解释性入门:模型为什么这么判?Python 实战 + 4 个最常见的误读
后端·python·机器学习
codists1 小时前
2026年8月文章一览
python
外收内放1 小时前
Python基础语法练习题(31-33)
开发语言·python
2601_962885721 小时前
如何用 Python 画 K 线图(蜡烛图)?(mplfinance + 均线 + 成交量)
开发语言·python
Y3815326622 小时前
行业动态周报:用新闻 API 追踪一个赛道的声音变化
python·搜索引擎
零域码客2 小时前
零基础Python爬虫入门:从HTTP协议到 requests + BeautifulSoup 实战
爬虫·python·http
“AI国潮设计-小江”2 小时前
【SDXL实战】用AI生成“财神爷蛋糕×英歌舞人物”潮汕国潮甜品IP,附Prompt与批量生成思路
开发语言·人工智能·python·aigc