pytesseract示例

# coding:utf-8
from selenium import webdriver
from time import sleep
import unittest
from PIL import Image
from PIL import ImageEnhance
import pytesseract
driver=webdriver.Firefox()
url="https://passport.baidu.com/?getpassindex"
driver.get(url)
driver.maximize_window()
driver.save_screenshot(r"E:\aa.png")  #截取當前網頁,該網頁有我們需要的驗證碼
imgelement = driver.find_element_by_xpath(".//*[@id='forgotsel']/div/div[3]/img")
#imgelement = driver.find_element_by_id("code")  #定位驗證碼
location = imgelement.location  #獲取驗證碼x,y軸座標
size=imgelement.size  #獲取驗證碼的長寬
coderange=(int(location['x']),int(location['y']),int(location['x']+size['width']),
           int(location['y']+size['height'])) #寫成我們需要截取的位置座標
i=Image.open(r"E:\aa.png") #打開截圖
frame4=i.crop(coderange)  #使用Image的crop函數,從截圖中再次截取我們需要的區域
frame4.save(r"E:\frame4.png")
i2=Image.open(r"E:\frame4.png")
imgry = i2.convert('L')   #圖像加強,二值化,PIL中有九種不同模式。分別爲1,L,P,RGB,RGBA,CMYK,YCbCr,I,F。L爲灰度圖像
sharpness =ImageEnhance.Contrast(imgry)#對比度增強
i3 = sharpness.enhance(3.0)  #3.0爲圖像的飽和度
i3.save("E:\\image_code.png")
i4=Image.open("E:\\image_code.png")
text=pytesseract.image_to_string(i2).strip() #使用image_to_string識別驗證碼
print text
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章