Python implementation extracts classification application instances by icon

Source: Internet
Author: User
This article describes the Python implementation according to the icon to extract the classification application, share to everyone for your reference.

Here's how:

#!/usr/bin/python #-*-coding:utf-8-*-import Image import win32ui import Win32gui def make_regalur_image (img, size = (at ()): Return img.resize (Size). CONVERT (' RGB ') def split_image (img, part_size = (up, UP)): 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_simil AR (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 (Imag E.open (LF)), Make_regalur_image (Image.open (RF)) Li.save (lf + ' _regalur.png ') ri.save (rf + ' _regalur.png ') fd = open (' Stat.csv ', ' W ') Fd.wri   Te (' \ 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, p, p): Draw.line ((0, I,, i), fill = ' #ff0000 ') draw.line ((i, 0, I,), 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, +, +) 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.jp  G '% (I, 1), ' test/test%d/%d.jpg '% (I, 2)) *100) # make_doc_data (' test/test4/1.jpg ', ' test/test4/2.jpg ')

Hopefully this article will help you with Python programming.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.