使用pytesseract时得先安装tesseract,tesseract-ocr和pytesseract
from PIL import Imagefrom PIL import ImageEnhanceimport pytesseractimport re# 如果报错加上下面两行代码,更改tesseract路径pytesseract.pytesseract.tesseract_cmd = 'D:\\Program Files\\Tesseract-OCR\\tesseract.exe'tessdata_dir_config = '--tessdata-dir "D:\\Program Files\\Tesseract-OCR\\tessdata"'im=Image.open("./img/10.jpg")im=im.convert('L')im.show()im=ImageEnhance.Contrast(im)im=im.enhance(1)#im = im.resize((300, 90))ltext = pytesseract.image_to_string(im)#去掉非法字符,只保留字母数字ltext = re.sub("\W", "", ltext)im.show()print(ltext)#print(pytesseract.image_to_string(im))#print(pytesseract.image_to_boxes(im))#print(im.format, im.size, im.mode)