pytesseract庫的安裝和使用
- 2020 年 1 月 19 日
- 筆記
在寫爬蟲的時候總是遇到一些以圖片的形式展示的資訊,因此要怎麼解析圖片上的資訊呢?在Google上查了一下,需要安裝pytesseract和pillow(我用的python3.7)和Tesseract-OCR
1. 安裝pytesseract
pip insatll pytesseract
2. 安裝pillow
pip install pillow
3. 安裝Tesseract-OCR(https://github.com/tesseract-ocr/tesseract)
4. 安裝完後將Tesseract-OCR的安裝路徑添加到環境變數中PATH和Path中都要添加
例如:

5. 在python的安裝路徑下的修改安裝的pytesseract庫裡面的pytesseract.py,將默認的改成Tesseract-OCR的安裝路徑

7. 配置完了開始擼程式碼吧

運行後發現會報錯

用Google查了一下,發現是因為驗證碼的圖片模式為RGBA,是無法分配調色盤給透明通道的。更換為RGB模式則不會出現該問題。
對原先的程式碼修改一下,變為:

修改後就能正常使用了。