pytesseract介紹
1.Python-tesseract是一個基於google's Tesseract-OCR的獨立封裝包;
2.Python-tesseract功能是識別圖片文件中文字,並作為返回參數返回識別結果;
3.Python-tesseract默認支持tiff、bmp格式圖片,只有在安裝PIL之后,才能支持jpeg、gif、png等其他圖片格式
pytesseract安裝
1.Python-tesseract支持python2.5及更高版本;
2.Python-tesseract需要安裝PIL(Python Imaging Library) ,來支持更多的圖片格式:
pip install pillow、pip install PIL
3.Python-tesseract需要安裝tesseract-ocr安裝包:Windows安裝Tesseract-OCR 4.00並配置環境變量
4.安裝pytesseract:
pip install pytesseract
pytesseract使用
使用步驟
> try: > import Image > except ImportError: > from PIL import Image > import pytesseract > print(pytesseract.image_to_string(Image.open('test.png'))) > print(pytesseract.image_to_string(Image.open('test-european.jpg'), lang='fra'))
識別二維碼
import pytesseract from PIL import Image import requests def Vercode(): url = "http://www.xxxx" header = {"user_agent":"Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)"} r =requests.get(url,headers=header,timeout=5) with open('vcode.jpg','wb') as pic: pic.write(r.content) im = pytesseract.image_to_string(Image.open('vcode.jpg')) im = im.replace(' ', '') if im != '': return im else: return Vercode() print Vcode()
refer: