1. 程式人生 > >[Python] 識別影象中的文字--pytesser模組

[Python] 識別影象中的文字--pytesser模組

安裝pytesser

  1. 安裝PIL

    pip2 install numpy
    pip2 install pillow
    
  2. C:\Python27\ArcGIS10.2\Lib\site-packages\pytesser
    
  3. pytesser.py檔名修改為__init__.py

  4. 開啟第三步的檔案

    import Image 
    #修改為 
    from PIL import Image
    
    tesseract_exe_name = 'tesseract'
    # 修改為
    tesseract_exe_name = r'C:\Python27\ArcGIS10.2\Lib\site-packages\pytesser\tesseract' # Name of executable to be called at command line
    
  5. 測試

    import pytesser
    # 未報錯即成功
    

使用

識別簡單英文與數字可以,識別中文不行

from pytesser import *
# 法一
im = Image.open(r'E:\user\Desktop\test_cmd.png')
text = image_to_string(im)
print text
# 法二
text = image_file_to_string(r'E:\user\Desktop\test_cmd.png',graceful_errors=True)
print text
# 使用ImageEnhance可以增強圖片的識別率
image = Image.open(r'E:\user\Desktop\test_cmd.png')
enhancer = ImageEnhance.Contrast(image)
image_enhancer = enhancer.enhance(4)
print image_to_string(image_enhancer)