Python 实现图片对比检测

在写测试框架的时候,需要用到图片对比的方法来判断用例执行的情况,问了一下度娘,原来可以用PIL模块处理:

  

       from PIL import Image  # 先安装Pillow, \>pip install Pillow, or \>easy_install Pillow ,参考:http://pillow.readthedocs.io/en/latest/installation.html
       import math
       import operator

   def imageSimilarity(self,image1, image2):
                
        image1 = Image.open(image1)
        image2 = Image.open(image2)
        h1 = image1.histogram()
        h2 = image2.histogram()
        SV = math.sqrt(reduce(operator.add, list(map(lambda a,b: (a-b)**2, h1, h2)))/len(h1) ) #完全相同时结果为0.0,差别越大值越大
        print SV
        return SV
    
    def checkImage(self, standardImage, similar=0.99):

        _dic={
            1 : 0,
            0.99 : 10,
            0.9 : 50,
            }
        
        if self.imageSimilarity(standardImage) > _dic[similar]:
            print("---Fail,图片对比失败")
            return False
        
        else:
            print("---pass,图片对比成功")
            return True

  PIL里还有很多处理图片的方法,用到的话再说,有兴趣的自己研究吧。

转载于:https://www.cnblogs.com/gaigaige/p/6520247.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值