python3.5配置pillow,pytesseract与selenium
来源:互联网 发布:滴滴面试经验 java 编辑:程序博客网 时间:2024/06/11 04:33
pip install pillow
pip install pytesseract
安装tesseract-ocr-setup-3.05.00dev.exe并配置环境变量,cmd输入tesseract可运行。
打开文件 pytesseract.py,找到如下代码,将tesseract_cmd的值修改为全路径,如下:
# CHANGE THIS IF TESSERACT IS NOT IN YOUR PATH, OR IS NAMED DIFFERENTLY
# tesseract_cmd = 'tesseract'
tesseract_cmd = 'C:/Program Files (x86)/Tesseract-OCR/tesseract.exe'
pip install selenium
selenium插件需要使用浏览器驱动,并配置浏览器驱动环境变量。本人使用谷歌浏览器,火狐也可以。
驱动放在谷歌浏览器安装根目录。谷歌浏览器驱动及对应版本 (在谷歌浏览器地址栏输入 chrome:version 查看谷歌浏览器版本信息)
http://chromedriver.storage.googleapis.com/index.html(谷歌浏览器驱动下载地址)
http://blog.csdn.net/huilan_same/article/details/51896672(对应版本)
示例代码:
from PIL import Image,ImageEnhance
import pytesseract
from selenium import webdriver
import os
#谷歌浏览器驱动位置
chromedriver = "C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe"
os.environ["webdriver.chrome.driver"] = chromedriver
driver = webdriver.Chrome(chromedriver)
url="****" #登陆页面地址
path1=r"C:\Users\黄\Desktop\test\vc1.png" #网页截图
path2=r"C:\Users\黄\Desktop\test\vc2.png" #验证码区域截图
driver.maximize_window() #将浏览器最大化
driver.get(url)
driver.save_screenshot(path1) #截取当前网页,该网页有我们需要的验证码
ck = driver.find_element_by_name('ck')#特征值
print(ck.get_attribute('value'))
imgelement = driver.find_element_by_id('verificationCodeImg')
location = imgelement.location #获取验证码x,y轴坐标
size=imgelement.size #获取验证码的长宽
print(location)
print(size)
#若不准备,在在线ps中打开图片,看验证码x,y坐标
rangle=(int(location['x']),int(location['y']),int(location['x']+size['width']),int(location['y']+size['height']))
i=Image.open(path1) #打开截图
frame4=i.crop(rangle) #使用Image的crop函数,从截图中再次截取我们需要的区域
frame4.save(path2)
im=Image.open(path2)
imgry = im.convert('L')#转换灰度图
imgry.show() #显示灰度图
text=pytesseract.image_to_string(imgry).strip()
sharpness =ImageEnhance.Contrast(imgry)#对比度增强
sharp_img = sharpness.enhance(2.0)
text=pytesseract.image_to_string(sharp_img).strip() #使用image_to_string识别验证码
vc=""
for i in text:
if i.isdigit(): #判断i是不是int
vc += i
elif i.isalpha(): #判断i是不是str
cv += i
else:
pass
print(text)
print(vc)
driver.quit()
pip install pytesseract
安装tesseract-ocr-setup-3.05.00dev.exe并配置环境变量,cmd输入tesseract可运行。
打开文件 pytesseract.py,找到如下代码,将tesseract_cmd的值修改为全路径,如下:
# CHANGE THIS IF TESSERACT IS NOT IN YOUR PATH, OR IS NAMED DIFFERENTLY
# tesseract_cmd = 'tesseract'
tesseract_cmd = 'C:/Program Files (x86)/Tesseract-OCR/tesseract.exe'
pip install selenium
selenium插件需要使用浏览器驱动,并配置浏览器驱动环境变量。本人使用谷歌浏览器,火狐也可以。
驱动放在谷歌浏览器安装根目录。谷歌浏览器驱动及对应版本 (在谷歌浏览器地址栏输入 chrome:version 查看谷歌浏览器版本信息)
http://chromedriver.storage.googleapis.com/index.html(谷歌浏览器驱动下载地址)
http://blog.csdn.net/huilan_same/article/details/51896672(对应版本)
示例代码:
from PIL import Image,ImageEnhance
import pytesseract
from selenium import webdriver
import os
#谷歌浏览器驱动位置
chromedriver = "C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe"
os.environ["webdriver.chrome.driver"] = chromedriver
driver = webdriver.Chrome(chromedriver)
url="****" #登陆页面地址
path1=r"C:\Users\黄\Desktop\test\vc1.png" #网页截图
path2=r"C:\Users\黄\Desktop\test\vc2.png" #验证码区域截图
driver.maximize_window() #将浏览器最大化
driver.get(url)
driver.save_screenshot(path1) #截取当前网页,该网页有我们需要的验证码
ck = driver.find_element_by_name('ck')#特征值
print(ck.get_attribute('value'))
imgelement = driver.find_element_by_id('verificationCodeImg')
location = imgelement.location #获取验证码x,y轴坐标
size=imgelement.size #获取验证码的长宽
print(location)
print(size)
#若不准备,在在线ps中打开图片,看验证码x,y坐标
rangle=(int(location['x']),int(location['y']),int(location['x']+size['width']),int(location['y']+size['height']))
i=Image.open(path1) #打开截图
frame4=i.crop(rangle) #使用Image的crop函数,从截图中再次截取我们需要的区域
frame4.save(path2)
im=Image.open(path2)
imgry = im.convert('L')#转换灰度图
imgry.show() #显示灰度图
text=pytesseract.image_to_string(imgry).strip()
sharpness =ImageEnhance.Contrast(imgry)#对比度增强
sharp_img = sharpness.enhance(2.0)
text=pytesseract.image_to_string(sharp_img).strip() #使用image_to_string识别验证码
vc=""
for i in text:
if i.isdigit(): #判断i是不是int
vc += i
elif i.isalpha(): #判断i是不是str
cv += i
else:
pass
print(text)
print(vc)
driver.quit()
阅读全文
0 0
- python3.5配置pillow,pytesseract与selenium
- Python3.5配置Selenium
- python-识别图片-安装及配置:tesseract+pytesseract+Pillow
- Python验证码识别 安装Pillow、tesseract-ocr与pytesseract模块的安装以及错误解决
- Mac python3.5 + Selenium 开发环境配置
- Python3+phantomjs+selenium配置
- Python——验证码识别 安装Pillow、tesseract-ocr与pytesseract模块的安装以及错误解决
- selenium python pytesseract 二值化图像
- mac python 配置pytesseract
- python+pillow+pytesseract+Tesseract-OCR验证码识别
- mac配置python3+selenium+chrome
- Python3 用Pillow代替PIL
- python3安装Pillow(PIL)
- CentOS7,Python3.6安装pillow
- python3.5 + selenium环境搭建
- Python3 Pillow生成简单验证码图片
- windows下配置python + pytesseract环境
- ubuntu python3 安装selenium及chrome的环境配置
- ArrayList和LinkedList的比较
- Mybatis获取连接工厂工具类
- 32位系统中short、short int、int、long int、long、long long所表示数值的范围
- UVA 327
- selenium webdriver 学习总结-元素定位(一)
- python3.5配置pillow,pytesseract与selenium
- jeesite使用数据库oracle12c
- 深入理解 Python 异步编程
- HotSpot C2编译器
- linux 下面SCP拷贝文件
- BZOJ 3932 CQOI2015 主席树
- BZOJTree Rotations(线段树的合并)
- WIN7 64 位旗舰版安装 CUDA 6.0 过程(显卡为NVIDIA GT 440)
- 437. Path Sum III