欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-11-16 12:17:28
本文实例讲述了python实现根据图标提取分类应用程序,分享给大家供大家参考。 具体方法如下: #!/usr/bin/python # -*- coding...

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