Python2比较当前图片跟图库哪个图片相似的方法示例
作者:lbxx 时间:2021-05-22 09:51:22
本文实例讲述了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
另附参考的
# -*- coding: utf-8 -*-
'''
Created on 2018年5月17日
'''
from selenium import webdriver
from PIL import Image
import requests
import time
import base64
import base64
import requests
from urllib import urlencode
import json
# requests.packages.urllib3.disable_warnings()
import datetime
from time import strftime
from time import sleep
from PIL import Image
# import pytesseract
from PIL import Image
import os
import cv2
from numpy import average, dot, linalg
import heapq
import collections
from lib.readConfig import Readconfig
conf=Readconfig()
filedir=conf.getConfigValue("filedir")
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 getMH(i1,i2):
a=getImgHash(i1)
b=getImgHash(i2)
dist = 0;
for i in range(0,len(a)):
if a[i]==b[i]:
dist=dist+1
return dist
def getImgHash(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 match1(a,rootdir):
list = os.listdir(rootdir)
li=[]
for i in list:
# print rootdir+"/"+i
re=getMH(a,rootdir+"/"+i)
li.append(re)
# print li
# print max(li)
b=str(li.index(max(li))+1)
return b
def g_code(pic):
dic={"1":"2","2":"3","3":"4","4":"5","5":"6","6":"7","7":"8","8":"9",
"9":"a","10":"b","11":"c","12":"d","13":"e","14":"f","15":"g","16":"h",
"17":"i","18":"j","19":"k","20":"m","21":"n","22":"p","23":"q","24":"r",
"25":"s","26":"t","27":"u","28":"v","29":"w","30":"x","31":"y","32":"z"}
img = Image.open(pic)
a=img.size[0]
b=img.size[1]
p1=filedir+r'eos_tdym/lib/pic/das1.png'
p2=filedir+r'eos_tdym/lib/pic/das2.png'
p3=filedir+r'eos_tdym/lib/pic/das3.png'
p4=filedir+r'eos_tdym/lib/pic/das4.png'
dir1=filedir+r'eos_tdym/lib/pic/pic1'
dir2=filedir+r'eos_tdym/lib/pic/pic2'
dir3=filedir+r'eos_tdym/lib/pic/pic3'
dir4=filedir+r'eos_tdym/lib/pic/pic4'
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)
re1=str(match1(p1,dir1))
re2=str(match1(p2,dir2))
re3=str(match1(p3,dir3))
re4=str(match1(p4,dir4))
print u"获取到验证码:"+dic[re1]+dic[re2]+dic[re3]+dic[re4]
return dic[re1],dic[re2],dic[re3],dic[re4]
def g_code1(pic):
dic={"1":"2","2":"3","3":"4","4":"5","5":"6","6":"7","7":"8","8":"9",
"9":"a","10":"b","11":"c","12":"d","13":"e","14":"f","15":"g","16":"h",
"17":"i","18":"j","19":"k","20":"m","21":"n","22":"p","23":"q","24":"r",
"25":"s","26":"t","27":"u","28":"v","29":"w","30":"x","31":"y","32":"z"}
img = Image.open(pic)
a=img.size[0]
b=img.size[1]
p1="pic5/das1.png"
p2="pic5/das2.png"
p3="pic5/das3.png"
p4="pic5/das4.png"
dir1="pic1"
dir2="pic2"
dir3="pic3"
dir4="pic4"
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)
re1=match1(p1,dir1)
re2=match1(p2,dir2)
re3=match1(p3,dir3)
re4=match1(p4,dir4)
print dic[re1]
print dic[re2]
print dic[re3]
print dic[re4]
return dic[re1],dic[re2],dic[re3],dic[re4]
希望本文所述对大家Python程序设计有所帮助。
来源:https://www.cnblogs.com/dmtz/p/11237955.html
标签:Python2,比较,图片相似
0
投稿
猜你喜欢
Flask和pyecharts实现动态数据可视化
2022-06-15 04:21:20
python如何重载模块实例解析
2021-10-13 03:47:56
Pytorch卷积神经网络resent网络实践
2022-01-21 02:08:42
Vue源码之关于vm.$delete()/Vue.use()内部原理详解
2023-07-02 16:48:08
Pytes正确的配置使用日志功能
2023-07-18 05:51:17
电子商务搜索LIST页面用户体验设计
2010-08-03 12:57:00
Python类的基本写法与注释风格介绍
2023-01-15 07:53:52
Python的加密模块md5、sha、crypt使用实例
2022-02-17 14:44:44
Web性能优化系列 10个提升JavaScript性能的技巧
2024-05-28 15:40:37
Python中matplotlib如何改变画图的字体
2023-02-19 16:46:38
使用python求解迷宫问题的三种实现方法
2022-10-25 01:16:35
使用面向对象的技术创建高级JavaScript Web 应用程序
2008-11-03 13:00:00
Python Flask前后端Ajax交互的方法示例
2021-09-07 09:09:21
WEB开发中合理选择图片格式
2011-09-22 20:32:06
Python3+selenium实现cookie免密登录的示例代码
2023-01-31 15:43:09
python3获取两个日期之间所有日期,以及比较大小的实例
2023-01-31 06:54:52
关于python的第三方库下载与更改方式
2021-02-28 10:32:55
用SQL语句查询数据库中某一字段下相同值的记录方法
2024-01-23 14:49:54
ie7空格的间距要比ie6/firefox/opera的都要大
2008-05-24 16:54:00
对python 多线程中的守护线程与join的用法详解
2021-08-11 10:56:51