1.安装Pillow
pip install Pillow
2.安装tesseract-ocr
github地址:https://github.com/tesseract-ocr/tesseract
或本地下载地址:https://
示例:
# -*-coding:utf-8-*- from PIL import Image import sys import os import pytesseractfrom selenium import webdriver sys.path.append('C:\Python27\Lib\site-packages\pytesser') import pytesser url='http://192.168.24.189/system/code?0.6824490785056669' driver = webdriver.Firefox() driver.maximize_window() #将浏览器最大化 driver.get(url) imgelement = driver.find_element_by_id('codeImg') #定位验证码 location = imgelement.location #获取验证码x,y轴坐标 size=imgelement.size #获取验证码的长宽 rangle=(int(location['x']),int(location['y']),int(location['x']+size['width']),int(location['y']+size['height'])) #写成我们需要截取的位置坐标 name="code.jpg" driver.find_element_by_id("codeImg").click() driver.save_screenshot(name) #截取当前网页,该网页有我们需要的验证码 aa=Image.open(name) #打开截图 frame4=aa.crop(rangle) #使用Image的crop函数,从截图中再次截取我们需要的区域 frame4.save(name) im = Image.open(name)#转化到灰度图imgry = im.convert('L')#保存图像imgry.save('g'+name)#二值化,采用阈值分割法,threshold为分割点threshold = 140table = []for j in range(256): if j < threshold: table.append(0) else: table.append(1)out = imgry.point(table, '1')out.save('b'+name)#识别text = pytesseract.image_to_string(out)#识别对吗text = text.strip()text = text.upper();print (text)text = pytesseract.image_to_string(Image.open('code.png'), lang="eng")print(text)
以上就是python3使用Pillow、tesseract-ocr与pytesseract模块的图片识别的方法的详细内容,更多关于python3 图片识别的资料请关注其它相关文章!