python实现根据图标提取分类应用程序实例


Posted in Python onSeptember 28, 2014

本文实例讲述了python实现根据图标提取分类应用程序,分享给大家供大家参考。

具体方法如下:

#!/usr/bin/python 
# -*- coding: utf-8 -*- 
 
import Image
 
import win32ui 
import win32gui 
 
def make_regalur_image(img, size = (256, 256)): 
  return img.resize(size).convert('RGB') 
 
def split_image(img, part_size = (64, 64)): 
  w, h = img.size 
  pw, ph = part_size 
   
  assert w % pw == h % ph == 0 
   
  return [img.crop((i, j, i+pw, j+ph)).copy() \
      for i in xrange(0, w, pw) \
      for j in xrange(0, h, ph)] 
 
def hist_similar(lh, rh): 
  assert len(lh) == len(rh) 
  return sum(1 - (0 if l == r else float(abs(l - r))/max(l, r)) for l, r in zip(lh, rh))/len(lh) 
 
def calc_similar(li, ri): 
#  return hist_similar(li.histogram(), ri.histogram()) 
  return sum(hist_similar(l.histogram(), r.histogram()) for l, r in zip(split_image(li), split_image(ri))) / 16.0 
       
 
def calc_similar_by_path(lf, rf): 
  li, ri = make_regalur_image(Image.open(lf)), make_regalur_image(Image.open(rf)) 
  return calc_similar(li, ri) 
 
def make_doc_data(lf, rf): 
  li, ri = make_regalur_image(Image.open(lf)), make_regalur_image(Image.open(rf)) 
  li.save(lf + '_regalur.png') 
  ri.save(rf + '_regalur.png') 
  fd = open('stat.csv', 'w') 
  fd.write('\n'.join(l + ',' + r for l, r in zip(map(str, li.histogram()), map(str, ri.histogram())))) 
#  print >>fd, '\n' 
#  fd.write(','.join(map(str, ri.histogram()))) 
  fd.close() 
  import ImageDraw 
  li = li.convert('RGB') 
  draw = ImageDraw.Draw(li) 
  for i in xrange(0, 256, 64): 
    draw.line((0, i, 256, i), fill = '#ff0000') 
    draw.line((i, 0, i, 256), fill = '#ff0000') 
  li.save(lf + '_lines.png') 
   
def getIcon(filename): 
 
  large, small = win32gui.ExtractIconEx(filename,0)# 
  win32gui.DestroyIcon(small[0]) 
   
  hdc = win32ui.CreateDCFromHandle( win32gui.GetDC(0) ) 
  hbmp = win32ui.CreateBitmap() 
  hbmp.CreateCompatibleBitmap( hdc, 32, 32 ) 
  hdc = hdc.CreateCompatibleDC() 
  hdc.SelectObject( hbmp ) 
  hdc.DrawIcon( (0,0), large[0] ) 
  hbmp.SaveBitmapFile( hdc, "save.bmp" ) 
     
if __name__ == '__main__': 
  #path = r'test/TEST%d/%d.JPG' 
  for i in range(1,4): 
    getIcon(r'test/TEST1/%d.exe' % i) 
    print 'RESULT:%d' % (calc_similar_by_path('save.bmp',r"test/TEST1/backup.bmp")*100) 
   
  #for i in xrange(1, 7): 
    #print 'test_case_%d: %.3f%%'%(i, \ 
      #calc_similar_by_path('test/TEST%d/%d.JPG'%(i, 1), 'test/TEST%d/%d.JPG'%(i, 2))*100) 
   
#  make_doc_data('test/TEST4/1.JPG', 'test/TEST4/2.JPG')

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

Python 相关文章推荐
python创建进程fork用法
Jun 04 Python
详解python进行mp3格式判断
Dec 23 Python
使用python实现个性化词云的方法
Jun 16 Python
Python实现绘制双柱状图并显示数值功能示例
Jun 23 Python
浅谈dataframe中更改列属性的方法
Jul 10 Python
python3去掉string中的标点符号方法
Jan 22 Python
python manage.py runserver流程解析
Nov 08 Python
Python 中使用 PyMySQL模块操作数据库的方法
Nov 10 Python
解决pycharm 安装numpy失败的问题
Dec 05 Python
Python如何输出警告信息
Jul 30 Python
Python Request类源码实现方法及原理解析
Aug 17 Python
Python .py生成.pyd文件并打包.exe 的注意事项说明
Mar 04 Python
wxPython事件驱动实例详解
Sep 28 #Python
python中的多重继承实例讲解
Sep 28 #Python
python错误处理详解
Sep 28 #Python
python中实现定制类的特殊方法总结
Sep 28 #Python
python之wxPython菜单使用详解
Sep 28 #Python
python中lambda函数 list comprehension 和 zip函数使用指南
Sep 28 #Python
python之wxPython应用实例
Sep 28 #Python
You might like
实测在class的function中include的文件中非php的global全局环境
2013/07/15 PHP
PHP使用pdo连接access数据库并循环显示数据操作示例
2018/06/05 PHP
php进程daemon化的正确实现方法
2018/09/06 PHP
PHP如何使用array_unshift()在数组开头插入元素
2020/09/01 PHP
Javascript操纵Cookie实现购物车程序
2007/02/15 Javascript
在JavaScript中使用inline函数的问题
2007/03/08 Javascript
一个判断email合法性的函数[非正则]
2008/12/09 Javascript
javascript 动态调整图片尺寸实现代码
2009/12/28 Javascript
JS函数验证总结(方便js客户端输入验证)
2010/10/29 Javascript
将字符串转换成gb2312或者utf-8编码的参数(js版)
2013/04/10 Javascript
javascript生成随机数的方法
2014/05/16 Javascript
js数组与字符串的相互转换方法
2014/07/09 Javascript
js 能实现监听F5页面刷新子iframe 而父页面不刷新的方法
2016/11/09 Javascript
JavaScript实现的选择排序算法实例分析
2017/04/14 Javascript
vue 粒子特效的示例代码
2017/09/19 Javascript
关于TypeScript模块导入的那些事
2018/06/12 Javascript
发布订阅模式在vue中的实际运用实例详解
2019/06/09 Javascript
JS中数据结构与算法---排序算法(Sort Algorithm)实例详解
2019/06/17 Javascript
nodejs脚本centos开机启动实操方法
2020/03/04 NodeJs
python的类方法和静态方法
2014/12/13 Python
python根据给定文件返回文件名和扩展名的方法
2015/03/27 Python
在Python中利用Pandas库处理大数据的简单介绍
2015/04/07 Python
浅谈Pandas Series 和 Numpy array中的相同点
2019/06/28 Python
利用python list完成最简单的DB连接池方法
2019/08/09 Python
解决Python计算矩阵乘向量,矩阵乘实数的一些小错误
2019/08/26 Python
Python requests模块安装及使用教程图解
2020/06/30 Python
浅析Python 责任链设计模式
2020/09/11 Python
Matplotlib配色之Colormap详解
2021/01/05 Python
TripAdvisor西班牙官方网站:全球领先的旅游网站
2018/01/10 全球购物
英国儿童鞋和靴子:Start-Rite
2019/05/06 全球购物
What's the difference between deep copy and shallow copy? (深拷贝与浅拷贝有什么区别)
2015/11/10 面试题
万年牢教学反思
2014/02/15 职场文书
学院党委班子四风问题自查报告及整改措施
2014/10/25 职场文书
旗帜观后感
2015/06/08 职场文书
城南旧事观后感
2015/06/11 职场文书
python实现简单反弹球游戏
2021/04/12 Python