PYTHON PYTESSER 的安装

来源:互联网 发布:怎么在淘宝上买东西 编辑:程序博客网 时间:2024/06/05 11:04

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-8from selenium import webdriverfrom 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 Keysfrom time import sleep#需要先安装PIL模块# pytesser提供了两种识别图片方法,通过image对象和图片地址from pytesser import *#from pytesser import pytesserfrom PIL import Imageimport os,timedriver = 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 存在",resultsize=driver.find_element_by_id("imgcode").sizeprint u"打印验证码图片的属性:",size#获取元素的文本text=driver.find_element_by_id("imgcode").textprint textlocation = 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')#旋转图片rotateprint 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 = 140table = []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()