將驗證碼圖片命名code.jpg放在項目根目錄下
import tesserocr
from PIL import Image
image = Image.open('code.jpg') #打開驗證碼圖片
# 模式L”爲灰色圖像,它的每個像素用8個bit表示,0表示黑,255表示白,其他數字表示不同的灰度。
image1=image.convert('L') #
threshold = 150 #設置灰度閾值,自己調整
table = []
for i in range(256):
if i < threshold :
table.append(0)
else:
table.append(1)
#二值化
image2 = image1.point(table,'1')
#image2.show()
#image1.show() #顯示轉化結果
result = tesserocr.image_to_text(image2)
print(result)
#print(tesserocr.file_to_text('code.jpg'))