Python2比较当前图片跟图库哪个图片相似的方法示例


Posted in Python onSeptember 28, 2019

本文实例讲述了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程序设计有所帮助。

Python 相关文章推荐
Python正则表达式匹配HTML页面编码
Apr 08 Python
Python中使用PyQt把网页转换成PDF操作代码实例
Apr 23 Python
浅述python2与python3的简单区别
Sep 19 Python
python实现控制台打印的方法
Jan 12 Python
PyQt5+Caffe+Opencv搭建人脸识别登录界面
Aug 28 Python
python入门之基础语法学习笔记
Feb 08 Python
python GUI库图形界面开发之PyQt5窗口背景与不规则窗口实例
Feb 25 Python
python的sys.path模块路径添加方式
Mar 09 Python
Python爬虫抓取指定网页图片代码实例
Jul 24 Python
python 检测nginx服务邮件报警的脚本
Dec 31 Python
python gui开发——制作抖音无水印视频下载工具(附源码)
Feb 07 Python
python推导式的使用方法实例
Feb 28 Python
python使用opencv实现马赛克效果示例
Sep 28 #Python
python打包成so文件过程解析
Sep 28 #Python
python基于FTP实现文件传输相关功能代码实例
Sep 28 #Python
python网络爬虫 Scrapy中selenium用法详解
Sep 28 #Python
在vscode中配置python环境过程解析
Sep 28 #Python
python爬虫 线程池创建并获取文件代码实例
Sep 28 #Python
python 单线程和异步协程工作方式解析
Sep 28 #Python
You might like
优化使用mysql存储session的php代码
2008/01/10 PHP
php执行sql语句的写法
2009/03/10 PHP
PHP 时间转换Unix时间戳代码
2010/01/22 PHP
phpMyAdmin 链接表的附加功能尚未激活的问题
2010/08/01 PHP
PHP下载远程图片并保存到本地方法总结
2016/01/22 PHP
ext for eclipse插件安装方法
2008/04/27 Javascript
JavaScript中关于indexOf的使用方法与问题小结
2010/08/05 Javascript
利用Javascript判断操作系统的类型实现不同操作系统下的兼容性
2013/01/29 Javascript
js实现弹窗插件功能实例代码分享
2013/12/12 Javascript
JavaScript针对网页节点的增删改查用法实例
2015/02/02 Javascript
js实现单击图片放大图片的方法
2015/02/17 Javascript
Javascript中的匿名函数与封装介绍
2015/03/15 Javascript
谈谈JavaScript中function多重理解
2015/08/28 Javascript
跟我学习JScript的Bug与内存管理
2015/11/18 Javascript
JS实现的仿淘宝交易倒计时效果
2015/11/27 Javascript
vue+ElementUI实现订单页动态添加产品数据效果实例代码
2017/07/13 Javascript
JavaScript创建对象的七种方式全面总结
2017/08/21 Javascript
一个有意思的鼠标点击文字特效jquery代码
2017/09/23 jQuery
vue返回上一页面时回到原先滚动的位置的方法
2018/12/20 Javascript
js之切换全屏和退出全屏实现代码实例
2019/09/09 Javascript
js实现经典贪吃蛇小游戏
2020/03/19 Javascript
Webpack中SplitChunksPlugin 配置参数详解
2020/03/24 Javascript
jQuery使用jsonp实现百度搜索的示例代码
2020/07/08 jQuery
[49:27]LGD vs OG 2018国际邀请赛小组赛BO2 第二场 8.16
2018/08/17 DOTA
Python+Django在windows下的开发环境配置图解
2009/11/11 Python
零基础写python爬虫之HTTP异常处理
2014/11/05 Python
python删除特定文件的方法
2015/07/30 Python
Python实现投影法分割图像示例(二)
2020/01/17 Python
求职自荐信
2013/12/14 职场文书
洗煤厂厂长岗位职责
2014/01/03 职场文书
商务会议邀请函
2014/01/09 职场文书
地球上的星星观后感
2015/06/02 职场文书
冲出亚马逊观后感
2015/06/03 职场文书
Mybatis-Plus进阶分页与乐观锁插件及通用枚举和多数据源详解
2022/03/21 Java/Android
mysql中数据库覆盖导入的几种方式总结
2022/03/25 MySQL
Python Pandas解析读写 CSV 文件
2022/04/11 Python