PYTHON PYTESSER 的安装

Python安装包:

需要安装的包主要有两个: PIL 和 pytesser 。 

 

PIL模块的安装

pytesser 模块的安装:

下载后得到 “pytesser_v0.0.1.zip”,是一个压缩文件,使用方法: 

1、在 “D:\For_Software\others\Python_PyDev\Python\Lib\site-packages” 路径下新建一个文件夹,命名 “pytesser” 。把 “pytesser_v0.0.1.zip” 里的文件解压到该目录:

2、将 “pytesser.py” 改名为 “__init__.py”。

3、打开 “__init__.py” 文件,将 “tesseract_exe_name” 变量的值改为 “‘D:/For_Software/others/Python_PyDev/Python/Lib/site-packages/pytesser/tesseract’”(原值为 “‘tesseract’”)。

4、pytesser 模块依赖于 PIL 模块,如果是按照上面的方法安装 PIL 的话,需要把 “init.py” 文件里的 “import Image” 改成 “from PIL import Image” 。 

#coding=utf-8
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException,TimeoutException
#from selenium.common.exceptions import   
from selenium.webdriver.support.ui import WebDriverWait # available since 
from selenium.webdriver.common.keys import Keys


from time import sleep


#需要先安装PIL模块




# pytesser提供了两种识别图片方法,通过image对象和图片地址


from pytesser import *


#from pytesser import pytesser
from PIL import Image


import os,time




driver = webdriver.Chrome()
print u"加载驱动完成.."
driver.get("http://osms.sit.sf-express.com:2080/dep/index.pub")#加载页面


driver.maximize_window() # 浏览器全屏显示


print u"最大化页面窗口完成.."


#用于截图能截取整个页面
#driver.set_window_size(1200, 900)


#print u"设置窗口尺寸为1200 * 900"


ISOTIMEFORMAT="%Y%m%d%H%M%S"
strTime = time.strftime( ISOTIMEFORMAT, time.localtime())


print u"加载页面完成.."


time.sleep(1)


driver.save_screenshot('E:\\pythonScript\\Codeimages\\'+strTime+'code.png')




print u"保存截图完成.."


#得到截取的验证码图片
#设置元素是否用户可见
result=driver.find_element_by_id("imgcode").is_displayed()


print u"验证码属性ID imgcode 存在",result


size=driver.find_element_by_id("imgcode").size


print u"打印验证码图片的属性:",size


#获取元素的文本
text=driver.find_element_by_id("imgcode").text


print text


location = driver.find_element_by_id("imgcode").location  
print location 


im = Image.open('E:\\pythonScript\\Codeimages\\'+strTime+'code.png')
'''
#旋转图片
fixedIm=im.rotate(270)
fixedIm.save('E:\\pythonScript\\Codeimages\\'+strTime+'rotate.png')
#旋转图片rotate
print u"旋转图片完成"
'''
left = driver.find_element_by_id("imgcode").location['x']
top = driver.find_element_by_id("imgcode").location['y']
right = driver.find_element_by_id("imgcode").location['x'] + driver.find_element_by_id("imgcode").size['width']
bottom = driver.find_element_by_id("imgcode").location['y'] + driver.find_element_by_id("imgcode").size['height']


im = im.crop((left, top, right, bottom))


print u"保存验证码图片完成"


im.save('E:\\pythonScript\\Codeimages\\'+strTime+'screenshot.png')
#识别图片中的验证码
os.remove('E:\\pythonScript\\Codeimages\\'+strTime+'code.png')
print u"删除截屏图片完成"
#**************************************************************************
codeimg = Image.open('E:\\pythonScript\\Codeimages\\'+strTime+'screenshot.png')
#把彩色图像转化为灰度图像。RBG转化到HSI彩色空间,采用I分量
imgry = codeimg.convert('L')
#打开图片,展示效果 imgry.show()
#二值化处理
'''
threshold = 140
table = []
for i in range(256):
    if i < threshold:
        table.append(0)
    else:
        table.append(1)
out = imgry.point(table, '1')
out.show()
'''
#imt = Image.open('E:\\pythonScript\\Codeimages\\20170524140427screenshot.png')  
print image_to_string(imgry)


print image_file_to_string('E:\\pythonScript\\Codeimages\\'+strTime+'screenshot.png')


imgry.show()




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值