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

 更新时间:2019年09月28日 11:09:38   作者:lbxx  
这篇文章主要介绍了Python2比较当前图片跟图库哪个图片相似的方法,结合实例形式分析了Python文件目录操作及图形运算相关使用技巧,需要的朋友可以参考下

本文实例讲述了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数据结构与算法教程》、《Python Socket编程技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总

希望本文所述对大家Python程序设计有所帮助。

相关文章

  • 详解Matplotlib绘图之属性设置

    详解Matplotlib绘图之属性设置

    这篇文章主要介绍了详解Matplotlib绘图之属性设置,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-08-08
  • django实现登录时候输入密码错误5次锁定用户十分钟

    django实现登录时候输入密码错误5次锁定用户十分钟

    这篇文章主要介绍了django实现登录时候输入密码错误5次锁定用户十分钟,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-11-11
  • python字符串连接方式汇总

    python字符串连接方式汇总

    这篇文章主要介绍了python字符串连接方式汇总,很实用的基础技巧汇总,需要的朋友可以参考下
    2014-08-08
  • 虚拟机下载python是否需要联网

    虚拟机下载python是否需要联网

    在本篇文章里小编给大家分享的是一篇关于虚拟机下载python是否需要联网的相关文章,有需要的朋友们可以参考下。
    2020-07-07
  • python如何将txt坐标批量打印到原图上

    python如何将txt坐标批量打印到原图上

    这篇文章主要介绍了python如何将txt坐标批量打印到原图上的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-08-08
  • python range实例用法分享

    python range实例用法分享

    在本篇文章里小编给大家整理了关于python range实例用法,有需要的朋友们可以学习参考下
    2020-02-02
  • Python导入Excel数据表的几种实现方式

    Python导入Excel数据表的几种实现方式

    在Python中可以使用许多库来处理Excel文件,下面这篇文章主要给大家介绍了关于Python导入Excel数据表的几种实现方式,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2024-01-01
  • python图的深度优先和广度优先算法实例分析

    python图的深度优先和广度优先算法实例分析

    这篇文章主要介绍了python图的深度优先和广度优先算法,结合实例形式分析了图的深度优先算法与广度优先算法相关概念、原理、实现技巧与操作注意事项,需要的朋友可以参考下
    2019-10-10
  • win与linux系统中python requests 安装

    win与linux系统中python requests 安装

    requests是Python的一个HTTP客户端库,跟urllib,urllib2类似,今天我们主要来谈谈win与linux系统中python requests的安装方法以及使用指南
    2016-12-12
  • 18个Python入门经典必背的程序分享

    18个Python入门经典必背的程序分享

    这篇文章主要为大家介绍了Python入门经典必背的18个程序。注意:这是初学者要牢记的 18 个代码,入门之后就简单了,快跟随小编一起来学习一下吧
    2023-02-02

最新评论