python 实现 图片对比 看是不是同一张图片 入门级别 有待提高

# -*- coding: utf-8 -*-
 
import colorsys
import re

#


def get_dominant_color(image):
     
   
    image = image.convert('RGBA') 
   
    image.thumbnail((200, 200)) 
    max_score = None
    dominant_color = None
     
    for count, (r, g, b, a) in image.getcolors(image.size[0] * image.size[1]):
        
        if a == 0:
            continue
         
        saturation = colorsys.rgb_to_hsv(r / 255.0, g / 255.0, b / 255.0)[1]
        
        y = min(abs(r * 2104 + g * 4130 + b * 802 + 4096 + 131072) >> 13, 235)
        
        y = (y - 16.0) / (235 - 16)
         
      
        if y > 0.9:
            continue
         
        # Calculate the score, preferring highly saturated colors.
        # Add 0.1 to the saturation so we don't completely ignore grayscale
        # colors by multiplying the count by zero, but still give them a low
        # weight.
        score = (saturation + 0.1) * count
         
        if score > max_score:
            max_score = score
            dominant_color = (r, g, b)
     
    return dominant_color
def get_count():
    cou=0
    i=0
    
    while(i<=9):
         i=i+1
         try:
             
             color1 = get_dominant_color(Image.open(r'C:\Users\lh9281\Desktop\bidui\1\%d.jpg'%i))
             color2 = get_dominant_color(Image.open(r'C:\Users\lh9281\Desktop\bidui\2\%d.jpg'%i))
         except:
             print "This file format is not support"
         arr1=(re.findall(r"\d+\.?\d*",str(color1)))
         arr2=(re.findall(r"\d+\.?\d*",str(color2)))
         a=abs(int(arr1[0])-int(arr2[0]))
         b=abs(int (arr1[1])-int (arr2[1]))
         c=abs(int (arr1[2])-int (arr2[2]))
         if a<=5 and b<=5 and c<=5:
             cou=cou+1
             print"count:"+str(cou)
         
         
    r=cou*100/i
    print str(i)+str(cou)+str(r)
    return r
     
if __name__=="__main__":
    from PIL import Image
    from PIL import ImageChops
    import os
    res=get_count()
    print"Detection accuracy"+str(res)+"%"
   


 
   
   


  

你可能感兴趣的:(python,测试)