Python 实现中英文 OCR 识别
🧠 Python 实现中英文 OCR 识别(含图像预处理)
一、🔧 所需环境安装
pip install pytesseract pillow opencv-python
安装 Tesseract OCR 引擎:
-
Linux (Ubuntu):
sudo apt update sudo apt install tesseract-ocr tesseract-ocr-chi-sim
-
Windows:
- 从官网安装:https://github.com/tesseract-ocr/tesseract
- 下载中文语言包 chi_sim.traineddata 放入
tessdata
目录
二、📸 图像预处理步骤
预处理有助于提升 OCR 识别效果,主要包括:
- 灰度化(去除颜色干扰)
- 高斯模糊(降噪)
- 二值化(增强对比度)
- 可选形态学操作(进一步去噪)
import cv2
from PIL import Image
def preprocess_image(image_path):
image = cv2.imread(image_path)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (3, 3), 0)
_, thresh = cv2.threshold(blurred, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
return Image.fromarray(thresh)
三、📝 OCR 识别代码(中英文支持)
import pytesseract
# Windows 用户请设置 Tesseract 的路径
# pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
def ocr_image(image_path):
image = preprocess_image(image_path)
text = pytesseract.image_to_string(image, lang='chi_sim+eng') # 中英文识别
return text
四、✅ 完整示例
import cv2
import pytesseract
from PIL import Image
def preprocess_image(image_path):
image = cv2.imread(image_path)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (3, 3), 0)
_, thresh = cv2.threshold(blurred, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
return Image.fromarray(thresh)
def ocr_image(image_path):
# image = preprocess_image(image_path)
# 打开图片
image = Image.open(image_path)
text = pytesseract.image_to_string(image, lang='chi_sim+eng')
return text
if __name__ == "__main__":
img_path = 'example.png' # 替换为你的图片路径
result = ocr_image(img_path)
print("识别结果:\n")
print(result)
🧩 小贴士
- OCR 效果依赖图像质量,尽量使用清晰、不压缩的图片
- 中文识别一定要安装中文语言包,并指定
'chi_sim'
- 图像背景干扰大时,可进一步增强预处理策略(如形态学操作)