python -m pip install --upgrade pip
pip install Pillow
pip install pytesseract
使用網路上找來的原碼
from PIL import Image
import pytesseract
img = Image.open('test1.png')
text = pytesseract.image_to_string(img, lang='eng')
print(text)
測試圖片
因版本問題會出現 error
pytesseract.pytesseract.TesseractNotFoundError: C:\Program Files (x86)\Tesseract-OCR esseract.exe is not installed or it's not in your PATH. See README file for more information.
此問題的解決
安裝完成後,將路徑複製下,在源碼處加上
pytesseract.pytesseract.tesseract_cmd = 'C:\Program Files (x86)\Tesseract-OCR\\tesseract.exe'
此處要留意路徑,一般預設如下,其中 \ 要有兩個 \\
'C:\Program Files (x86)\Tesseract-OCR\\tesseract.exe'
最後結果源碼:
from PIL import Image
import pytesseract
pytesseract.pytesseract.tesseract_cmd = 'C:\Program Files (x86)\Tesseract-OCR\\tesseract.exe'
img = Image.open('test.jpeg')
text = pytesseract.image_to_string(img, lang='eng')
print(text)