def getType(browser): """ Identifying the graphic path """ ttype = '' time.sleep(3.5) im0 = Image.open(io.BytesIO(browser.get_screenshot_as_png())) box = browser.find_element_by_id('patternCaptchaHolder') im = im0.crop((int(box.location['x']) + 10, int(box.location['y']) + 100, int(box.location['x']) + box.size['width'] - 10, int(box.location['y']) + box.size['height'] - 10)).convert('L') newBox = getExactly(im) im = im.crop(newBox) width = im.size[0] height = im.size[1] for png in ims.keys(): isGoingOn = True for i in range(width): for j in range(height): if ((im.load()[i, j] >= 245 and ims[png][i][j] < 245) or (im.load()[i, j] < 245 and ims[png][i][j] >= 245)) and abs(ims[png][i][j] - im.load()[i, j]) > 10: # With 245 as the threshold, about 245 as the blank, less than 245 as the line; the difference between two pixels is about 10 to remove the error on the 245 boundary isGoingOn = False break if isGoingOn is False: ttype = '' break else: ttype = png else: break px0_x = box.location['x'] + 40 + newBox[0] px1_y = box.location['y'] + 130 + newBox[1] PIXELS.append((px0_x, px1_y)) PIXELS.append((px0_x + 100, px1_y)) PIXELS.append((px0_x, px1_y + 100)) PIXELS.append((px0_x + 100, px1_y + 100)) return ttype
def getType(browser): """ 识别图形路径 """ ttype = '' time.sleep(3.5) im0 = Image.open(StringIO.StringIO(browser.get_screenshot_as_png())) box = browser.find_element_by_id('patternCaptchaHolder') im = im0.crop((int(box.location['x']) + 10, int(box.location['y']) + 100, int(box.location['x']) + box.size['width'] - 10, int(box.location['y']) + box.size['height'] - 10)).convert('L') newBox = getExactly(im) im = im.crop(newBox) width = im.size[0] height = im.size[1] print '########### start ', time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) diff_dict = {} for png in ims.keys(): for i in range(width): for j in range(height): # logging.info('im.load()[%s, %s] %s ims[%s][%s][%s] %s' % (i, j, im.load()[i, j], png,i, j, ims[png][i][j])) # if ((im.load()[i, j] >= 245 and ims[png][i][j] < 245) or (im.load()[i, j] < 245 and ims[png][i][j] >= 245)) and abs(ims[png][i][j] - im.load()[i, j]) > 10: # 以245为临界值,大约245为空白,小于245为线条;两个像素之间的差大约10,是为了去除245边界上的误差 if im.load()[i, j] != ims[png][i][j]: if png not in diff_dict.keys(): diff_dict[png] = 1 else: diff_dict[png] += 1 print '########### end ', time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) ttype = sorted(diff_dict.iteritems(), key = lambda x:x[1])[0][0] px0_x = box.location['x'] + 40 + newBox[0] px1_y = box.location['y'] + 130 + newBox[1] PIXELS.append((px0_x, px1_y)) PIXELS.append((px0_x + 100, px1_y)) PIXELS.append((px0_x, px1_y + 100)) PIXELS.append((px0_x + 100, px1_y + 100)) print '#########################', ttype, diff_dict return ttype
def getType(browser): """ 识别图形路径 """ ttype = '' time.sleep(3.5) im0 = Image.open(io.BytesIO(browser.get_screenshot_as_png())) try: box = browser.find_element_by_id('patternCaptchaHolder') except: return ttype im = im0.crop((int(box.location['x']) + 10, int(box.location['y']) + 100, int(box.location['x']) + box.size['width'] - 10, int(box.location['y']) + box.size['height'] - 10)).convert('L') newBox = getExactly(im) im = im.crop(newBox) width = im.size[0] height = im.size[1] for png in ims.keys(): isGoingOn = True for i in range(width): for j in range(height): if ((im.load()[i, j] >= 245 and ims[png][i][j] < 245) or (im.load()[i, j] < 245 and ims[png][i][j] >= 245)) and abs(ims[png][i][j] - im.load()[i, j]) > 10: # 以245为临界值,大约245为空白,小于245为线条;两个像素之间的差大约10,是为了去除245边界上的误差 isGoingOn = False break if isGoingOn is False: ttype = '' break else: ttype = png else: break px0_x = box.location['x'] + 40 + newBox[0] px1_y = box.location['y'] + 130 + newBox[1] PIXELS.append((px0_x, px1_y)) PIXELS.append((px0_x + 100, px1_y)) PIXELS.append((px0_x, px1_y + 100)) PIXELS.append((px0_x + 100, px1_y + 100)) return ttype
def getType(browser): """ 识别图形路径 """ ttype = '' time.sleep(3.5) im0 = Image.open(StringIO.StringIO(browser.get_screenshot_as_png())) box = browser.find_element_by_id('patternCaptchaHolder') im = im0.crop((int(box.location['x']) + 10, int(box.location['y']) + 100, int(box.location['x']) + box.size['width'] - 10, int(box.location['y']) + box.size['height'] - 10)).convert('L') newBox = getExactly(im) im = im.crop(newBox) width = im.size[0] height = im.size[1] for png in ims.keys(): isGoingOn = True for i in range(width): for j in range(height): if ((im.load()[i, j] >= 245 and ims[png][i][j] < 245) or (im.load()[i, j] < 245 and ims[png][i][j] >= 245)) and abs(ims[png][i][j] - im.load()[i, j]) > 10: # 以245为临界值,大约245为空白,小于245为线条;两个像素之间的差大约10,是为了去除245边界上的误差 isGoingOn = False break if isGoingOn is False: ttype = '' break else: ttype = png else: break px0_x = box.location['x'] + 40 + newBox[0] px1_y = box.location['y'] + 130 + newBox[1] PIXELS.append((px0_x, px1_y)) PIXELS.append((px0_x + 100, px1_y)) PIXELS.append((px0_x, px1_y + 100)) PIXELS.append((px0_x + 100, px1_y + 100)) return ttype
def loadims_to_matrix(): for png in ims.keys(): piclist = [] #show_img(ims[png],""+str(png)) for i in range(160): for j in range(160): piclist.append(ims[png][i][j]) ims_dict[png] = np.array(piclist)
def getType(browser): """ 识别图形路径 """ ttype = '' time.sleep(3.5) # im0 = Image.open(io.BytesIO(browser.get_screenshot_as_png())) browser.save_screenshot("aa.png") im0 = Image.open("aa.png") box = browser.find_element_by_id('patternCaptchaHolder')#获取图形验证码对话框部分 # im = im0.crop((int(box.location['x']) + 10, int(box.location['y']) + 100, int(box.location['x']) + box.size['width'] - 10, int(box.location['y']) + box.size['height'] - 10)).convert('L') im0.crop((int(box.location['x']) + 10, \ int(box.location['y']) + 100, \ int(box.location['x']) + box.size['width'] - 10, \ int(box.location['y']) + box.size['height'] - 10)).save("bb.png")#(left, upper, right, lower) im = Image.open("bb.png").convert("L")#转换为灰色图像 im.save("cc.png") newBox = getExactly(im) im.crop(newBox).save("dd.png") im = im.crop(newBox) width = im.size[0] height = im.size[1] min_dist = sys.maxsize#python int类型支持的最大值 for png in ims.keys(): # isGoingOn = True distance = 0 for i in range(width): for j in range(height): print("===========print(im.load()[i, j])============") print(im.load()[i, j]) distance += numpy.square(im.load()[i, j] - ims[png][i][j])# numpy.square计算各元素的平方 if (distance != 0): distance = numpy.sqrt(distance)#sqrt计算平方根 if (distance <= min_dist): min_dist = distance ttype = png # for i in range(width): # for j in range(height): # if ((im.load()[i, j] >= 245 and ims[png][i][j] < 245) or (im.load()[i, j] < 245 and ims[png][i][j] >= 245)) and abs(ims[png][i][j] - im.load()[i, j]) > 10: # 以245为临界值,大约245为空白,小于245为线条;两个像素之间的差大约10,是为了去除245边界上的误差 # isGoingOn = False # break # if isGoingOn is False: # ttype = '' # break # else: # ttype = png # else: # break px0_x = box.location['x'] + 40 + newBox[0] px1_y = box.location['y'] + 130 + newBox[1] PIXELS.append((px0_x, px1_y)) PIXELS.append((px0_x + 100, px1_y)) PIXELS.append((px0_x, px1_y + 100)) PIXELS.append((px0_x + 100, px1_y + 100)) print("------PIXELS--------") print(PIXELS) return ttype