python+selenium图片验证码破解代码成功率 90%
import numpy as np import cv2 as cv
def get_element_slide_distance(imagePath): # 读取图片 # imagePath = “D:pythonimage” bg = cv.imread(imagePath + ‘gisoracle.jpg’) front = cv.imread(imagePath + ‘small.jpg’)
# 灰度处理,是为了提高我们的效率,类似于降维打击 bg = cv.cvtColor(bg, cv.COLOR_BGR2GRAY) # 将blue,green,red转为gray front = cv.cvtColor(front, cv.COLOR_BGR2GRAY) # 将blue,green,red转为gray # 对滑块处理,我们下载出来滑块图片,发现上下多了一对空白,经过灰度会将此空白变成黑色,如果用此图片匹配背景会有问题,因此需要切除 front = front[front.any(1)] # 1是要灰度后边比较亮的,黑色就是暗的数字0,因此这样做会将小块儿留下来 # 用滑块匹配背景图 # cv.TM_CCOEFF_NORMED算法,匹配精度最高,时间最慢,会将滑块每个像素点与背景图像素对比,返回一个每个像素点匹配相似度的矩阵,寻找相似度最大的 result = cv.matchTemplate(bg, front, cv.TM_CCOEFF_NORMED) yiwei_max_loc = np.argmax(result) # 返回矩阵的最大一维位置,但是我们需要二维的位置 x, y = np.unravel_index(yiwei_max_loc, result.shape) # 会将一维的位置逆向根据二维的形状,返回位置横纵坐标 return y