这里我们用到了pytesseract库,下面是安装教程:
不能直接用pip安装会失败
我们首先下载 tesseract-ocr 地址:https://github.com/tesseract-ocr/tesseract/wiki
下载完.exe 安装并下载中文包
然后在pycharm中安装模块pytesseract
点击查看源码
然后将源码中的:
tesseract_cmd = 'tesseract'
更改为:
tesseract_cmd = r'C:\Program Files (x86)\Tesseract-OCR\tesseract.exe'
代码如下:
通过截取网址的元素截图 识别验证码
# coding=utf-8
from PIL import Image
import pytesseract
import time
from selenium import webdriver
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://so.gushiwen.org/user/login.aspx?from=http://so.gushiwen.org/user/collect.aspx")
time.sleep(5)
driver.save_screenshot("code.png")
element = driver.find_element_by_id("imgCode")
print(element.location)
print(element.size)
left = element.location['x']
top = element.location['y']
right = element.location['x'] + element.size['width']
bottom = element.location['y'] + element.size['height']
im = Image.open("code.png")
im = im.crop((left, top, right, bottom))
im.save("code.png")
im.show()
print("验证码截取成功")
driver.quit()
img = Image.open("code.png")
Img = img.convert("L")
threshold = 140
table = []
for i in range(256):
if i < threshold:
table.append(0)
else:
table.append(1)
pho = Img.point(table, '1')
i = pho.convert('RGB')
i.show()
print(pytesseract.image_to_string(i))
这也是简单的识别。那遇到复杂的图形识别我们应该这么做:
•灰度处理
•增加对比度(可选)
•二值化
•降噪
•倾斜校正分割字符
•建立训练库
•识别