天天看點

Python3.7 pytesseract+PIL+Tesseract-OCR識别中文

環境:

python版本為3.7.0

Tesseract-OCR版本:

Python3.7 pytesseract+PIL+Tesseract-OCR識别中文
  1. 首先安裝pytesseract、PIL

    cmd

    pip install pytesseract
    pip install PIL
               
  2. 然後安裝Tesseract-OCR

    安裝包

    帶DEV的是開發版本,不帶dev的是穩定版本

    安裝的時候需要選擇中文簡體資料包

    安裝後在檔案中有此檔案chi_sim.traineddata

    參考:

    安裝Tesseract-OCR

    Python3.7 pytesseract+PIL+Tesseract-OCR識别中文
  3. 添加環境變量
    Python3.7 pytesseract+PIL+Tesseract-OCR識别中文
    Python3.7 pytesseract+PIL+Tesseract-OCR識别中文
    Python3.7 pytesseract+PIL+Tesseract-OCR識别中文
  4. 使用
import pytesseract
import PIL
from PIL import Image
#中文需要添加
print (pytesseract.image_to_string(Image.open('1.jpg'),lang="chi_sim"))
           
Python3.7 pytesseract+PIL+Tesseract-OCR識别中文

遇到一個坑爹的問題,圖檔高度小于20像素的居然識别不出來字元,坑啊。。。。。。

Python3.7 pytesseract+PIL+Tesseract-OCR識别中文

就這個圖

########讀取值
#check_code=pytesseract.image_to_string(Image.open('hr_pic_1.png'),)#中文
check_code=pytesseract.image_to_string(Image.open('hr_1119.png'),lang="eng+equ")#預設英文,eng+equ:英文字元+數學字元
print (check_code.replace(" ","").replace("  ","").replace("'","").replace(".",""))
           

圖檔的屬性:

Python3.7 pytesseract+PIL+Tesseract-OCR識别中文

換一張:

Python3.7 pytesseract+PIL+Tesseract-OCR識别中文
Python3.7 pytesseract+PIL+Tesseract-OCR識别中文

emmmm。。。。不知道咋說,反正就很煩躁,搞的我解除安裝了幾個版本的Tesseract-OCR…

然後我又喪(閑)心(得)病(無)狂(聊)的測試了其他像素的。。。。

Python3.7 pytesseract+PIL+Tesseract-OCR識别中文

以上得出結論,寬度>=80像素,高度>=20像素,才能識别字元。。。。