在写测试框架的时候,需要用到图片对比的方法来判断用例执行的情况,问了一下度娘,原来可以用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里还有很多处理图片的方法,用到的话再说,有兴趣的自己研究吧。