Python2比较当前图片跟图库哪个图片相似的方法示例
作者:lbxx 发布时间:2021-05-22 09:51:22
标签:Python2,比较,图片相似
本文实例讲述了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
0
投稿
猜你喜欢
- 一、介绍ADB(Android调试桥)是一个命令行工具(CLI),可用于控制Android设备并与之通信。您可以执行许多操作,例如安装应用程
- 原理采用Python requests发起请求监测的URL,检测Http响应状态及是否超时,如果Http状态异常或响应超时,则通过聚合云推的
- <pre name="code" class="python">creazy.txt文件
- 一、查看存储过程存储过程创建以后,用户可以通过SHOW STATUS语句来查看存储过程的状态,也可以通过SHOW CREATE语句来查看存储
- 本文实例讲述了js网页滚动条滚动事件用法。分享给大家供大家参考。具体分析如下:在做js返回顶部的效果时,要监听网页滚动条滚动事件,这个事件就
- 迭代器聊迭代器前我们要先清楚迭代的概念:通常来讲从一个对象中依次取出数据,这个过程叫做遍历,这个手段称为迭代(重复执行某一段代码块,并将每一
- 此文刊登在《程序员》三月期,有删改提到安全问题,首先想到应付这些问题的应该是系统管理员以及后台开发工程师们,而前端开发工程师似乎离这些问题很
- 前言:测试过程中获取App相关log后,如何快速找出crash的部分,并导出到新的文件呢?感兴趣的话,继续往下看吧~思路:遍历多个日志文件,
- 在循环对象和函数对象中,我们了解了循环器(iterator)的功能。循环器是对象的容器,包含有多个对象。通过调用循环器的next()方法 (
- <?php $fp = fopen("http://www.***.com/**
- HTML表单是网站交互性的经典方式。 本章将介绍如何用Django对用户提交的表单数据进行处理。一、HTTP 请求HTTP协议以"
- 将解压文件放置C盘配置环境变量 Patch:C:\mysql-5.7.25-winx64\bin;安装mysql切换到bin目录 cd C:
- 前言入门爬虫很容易,几行代码就可以,可以说是学习 Python 最简单的途径。刚开始动手写爬虫,你只需要关注最核心的部分,也就是先成功抓到数
- 导语一直以来,中国象棋都是中华民族的一种象征,当然也是人们最为喜感的一种娱乐方式。在若干年前,人们都习惯于约上自己的棋友,来一种激战。可是,
- 前言:工作中遇到以下小问题,解决方法如下,可能比较暴力,暂时留档,再进行优化。要求:将列表中json的 ‘id&
- 组合集总计: group by with rollup/cube grouping sets 子查询按执行方式分:标准子查询、关联子查询 标
- 前言:目前在研究易信公众号,想给公众号增加一个获取个人交通违章的查询菜单,通过点击返回查询数据。以下是实施过程。一、首先,用火狐浏览器打开X
- Insert 和 Update假设现在你要把下面的数据插入到数据库中.ID = 3TheDate=mktime(0,0,0,8,31,200
- 前言本方案只适应于小的项目、项目未上线或者紧急情况下可采用这种方式,一旦开启慢日志查询会增加数据库的压力,所以一般采用后台对数据操作时间写入
- linux下mysql默认是要区分表名大小写的。mysql是否区分大小写设置是由参数lower_case_table_names决定的,其中