Python学不会来打我(98)python识别图片上的文字
以下是使用Python识别图片文字的完整方案,整合Tesseract OCR、CnOCR等主流工具的实现方法与技术要点:#python##python自学##python教程#
一、核心工具对比
二、Tesseract OCR实现步骤
1. 环境安装
# 安装引擎(Windows需下载exe安装包)
brew install tesseract # MacOS
sudo apt install tesseract-ocr # Linux
# 安装Python依赖
pip install pytesseract pillow opencv-python
2. 基础识别代码
from PIL import Image
import pytesseract
import cv2
def ocr_with_tesseract(image_path, lang='chi_sim+eng'):
# 预处理(灰度化+二值化)
img = cv2.imread(image_path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_, binary = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY_INV)
# 识别文字
text = pytesseract.image_to_string(binary, lang=lang)
return text.strip()
print(ocr_with_tesseract("example.png"))