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创建一个最简单http webserver服务器的方法
May 08 Python
PHP网页抓取之抓取百度贴吧邮箱数据代码分享
Apr 13 Python
Python开发SQLite3数据库相关操作详解【连接,查询,插入,更新,删除,关闭等】
Jul 27 Python
pandas按若干个列的组合条件筛选数据的方法
Apr 11 Python
用TensorFlow实现戴明回归算法的示例
May 02 Python
Python实现字典(dict)的迭代操作示例
Jun 05 Python
详解python3安装pillow后报错没有pillow模块以及没有PIL模块问题解决
Apr 17 Python
linux环境中没有网络怎么下载python
Jul 07 Python
用Pelican搭建一个极简静态博客系统过程解析
Aug 22 Python
使用python 对验证码图片进行降噪处理
Dec 18 Python
xadmin使用formfield_for_dbfield函数过滤下拉表单实例
Apr 07 Python
keras 自定义loss损失函数,sample在loss上的加权和metric详解
May 23 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
PHP5各个版本的新功能和新特性总结
2014/03/16 PHP
PHP函数strip_tags的一个bug浅析
2014/05/22 PHP
用php和jQuery来实现“顶”和“踩”的投票功能
2016/10/13 PHP
thinkPHP5.0框架事务处理操作简单示例
2018/09/07 PHP
PHP通过调用新浪API生成t.cn格式短网址链接的方法详解
2019/02/20 PHP
可实现多表单提交的javascript函数
2007/08/01 Javascript
JavaScript 自动分号插入(JavaScript synat:auto semicolon insertion)
2009/11/04 Javascript
关于Javascript模块化和命名空间管理的问题说明
2010/12/06 Javascript
js中一维数组和二位数组中的几个问题示例说明
2014/07/17 Javascript
JavaScript避免内存泄露及内存管理技巧
2014/09/05 Javascript
修改或扩展jQuery原生方法的代码实例
2015/01/13 Javascript
深入探究使JavaScript动画流畅的一些方法
2015/06/30 Javascript
BootStrap tooltip提示框使用小结
2016/10/26 Javascript
移动端使用localResizeIMG4压缩图片
2017/04/22 Javascript
微信小程序canvas写字板效果及实例
2017/06/15 Javascript
jQuery序列化后的表单值转换成Json
2017/06/16 jQuery
vue引入jq插件的实例讲解
2017/09/12 Javascript
详解使用Next.js构建服务端渲染应用
2018/07/10 Javascript
浅谈vue中组件绑定事件时是否加.native
2019/11/09 Javascript
vue+canvas实现移动端手写签名
2020/05/21 Javascript
js+canvas实现刮刮奖功能
2020/09/13 Javascript
Python 模板引擎的注入问题分析
2017/01/01 Python
python去除文件中重复的行实例
2018/06/29 Python
django小技巧之html模板中调用对象属性或对象的方法
2018/11/30 Python
Python第三方包PrettyTable安装及用法解析
2020/07/08 Python
video结合canvas实现视频在线截图功能
2018/06/25 HTML / CSS
美国在线眼镜商城:Eyeglasses.com
2017/06/26 全球购物
北欧最好的童装网上商店:Babyshop
2019/09/15 全球购物
澳大利亚最好的电动自行车:Leon Cycle
2020/12/19 全球购物
火车来了教学反思
2014/02/11 职场文书
奉献家乡演讲稿
2014/09/16 职场文书
2014年党风廉政工作总结
2014/12/03 职场文书
给老婆的检讨书1000字
2015/01/01 职场文书
升职自荐信范文
2015/03/27 职场文书
MySQL慢查询优化解决问题
2022/03/17 MySQL
Mac电脑OS系统下安装Nginx的详细教程
2022/04/14 Servers