ocr_utils.py 666 B

12345678910111213141516171819202122
  1. import pytesseract
  2. from PIL import Image
  3. import cv2
  4. import numpy as np
  5. def extract_text_from_image(image_path):
  6. """从图片中提取文本"""
  7. # 使用OpenCV读取图像
  8. img = cv2.imread(image_path)
  9. gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  10. # 使用PIL将灰度图像转换为二值图像(黑白)
  11. _, thresh = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY_INV)
  12. # 使用pytesseract进行OCR
  13. text = pytesseract.image_to_string(thresh, lang='chi_sim') # 支持中文
  14. return text.strip()
  15. # 示例调用
  16. if __name__ == "__main__":
  17. text = extract_text_from_image('path_to_image.png')
  18. print("Extracted Text:", text)