python验证码识别PIL+pytesseract

1.需要模块安装

   在python安装目录scripts即:

   执行pip install pillow

  下载tesseract-ocr-setup-4.00.00dev.exe 安装,我的目录在C盘默认

  执行pip install pytesseract

2.上传测试案例

       

3.示例代码

 图片处理过程:

 1 from PIL import Image
 2 from pytesseract import *
 3 import PIL.ImageOps
 4 
 5 def initTable(threshold=140):
 6     table = []
 7     for i in range(256):
 8         if i < threshold:
 9             table.append(0)
10         else:
11             table.append(1)
12     return table
13 
14 im = Image.open('new.jpg')
15 #图片的处理过程
16 im = im.convert('L')
17 #像素点处理 二值图像,非黑即白 相当于去噪操作
18 binaryImage = im.point(initTable() , '1')
19 #binaryImage.show()
20 #模式“L”为灰色图像,它的每个像素用8个bit表示,0表示黑,255表示白,其他数字表示不同的灰度 
21 imgl = binaryImage.convert('L')
22 #输入图像转换为反色图像
23 imginvert = PIL.ImageOps.invert(imgl)
24 #imginvert.show()
25 vercode = pytesseract.image_to_string(imginvert)
26 print (vercode)

识别结果:

原文地址:https://www.cnblogs.com/linsu/p/8458961.html