Python2-比较当前图片跟图库那个图片相似
作者:互联网
# -*- coding: utf-8 -*- ''' Created on 2019年7月22日 ''' from selenium import webdriver from time import sleep from PIL import Image import random import os import cv2 import numpy as np url ="URL" driver = webdriver.Chrome() driver.implicitly_wait(10) driver.maximize_window() driver.get(url) sleep(2) driver.save_screenshot("E:/test/das.png") p1=r'E:/test/das1.png' p2=r'E:/test/das2.png' p3=r'E:/test/das3.png' p4=r'E:/test/das4.png' element = driver.find_element_by_id("imgcode") left = element.location['x'] top = element.location['y'] right = element.location['x'] + element.size['width'] bottom = element.location['y'] + element.size['height'] im1 = Image.open(r'E:/test/das.png') im1 = im1.crop((left, top, right, bottom)) im1.save(r"E:/test/dascode.png") img = Image.open("E:/test/dascode.png") cropped = img.crop((0, 0, 25, 30)) # (left, upper, right, lower) cropped.save(p1) cropped = img.crop((25, 0, 50, 30)) # (left, upper, right, lower) cropped.save(p2) cropped = img.crop((50, 0, 75, 30)) # (left, upper, right, lower) cropped.save(p3) cropped = img.crop((75, 0, 96, 30)) # (left, upper, right, lower) cropped.save(p4) def getGray(image_file): tmpls=[] for h in range(0, image_file.size[1]):#h for w in range(0, image_file.size[0]):#w tmpls.append( image_file.getpixel((w,h)) ) return tmpls def getAvg(ls):#获取平均灰度值 return sum(ls)/len(ls) def aHash(fne): image_file = Image.open(fne) # 打开 image_file=image_file.resize((35,35))#重置图片大小我12px X 12px image_file=image_file.convert("L")#转256灰度图 Grayls=getGray(image_file)#灰度集合 avg=getAvg(Grayls)#灰度平均值 bitls=''#接收获取0或1 for h in range(1, image_file.size[1]-1):#h for w in range(1, image_file.size[0]-1):#w if image_file.getpixel((w,h))>=avg:#像素的值比较平均值 大于记为1 小于记为0 bitls=bitls+'1' else: bitls=bitls+'0' return bitls def getMH(i1,i2): a=aHash(i1) b=aHash(i2) dist = 0; for i in range(0,len(a)): if a[i]==b[i]: dist=dist+1 return dist def match(a,rootdir): list = os.listdir(rootdir) li=[] for i in list: re=getMH(a,rootdir+"/"+i) li.append(re) b=str(li.index(max(li))+1) a=li.index(max(li)) return b,list[a].split(".")[0] a=match('E:/test/das4.png',"E:/test/pic4") print a
标签:image,图库,cropped,element,file,test,图片,png,Python2 来源: https://www.cnblogs.com/dmtz/p/11237955.html