from PIL import Image
from PIL import ImageChops 

def compare_images(path_one, path_two, diff_save_location):
    """
    比较图片,如果有不同则生成展示不同的图片

    @参数一: path_one: 第一张图片的路径
    @参数二: path_two: 第二张图片的路径
    @参数三: diff_save_location: 不同图的保存路径
    """
    image_one = Image.open(path_one)
    image_two = Image.open(path_two)
    try:
        diff = ImageChops.difference(image_one, image_two)

        if diff.getbbox() is None:
        # 图片间没有任何不同则直接退出
            print("【+】We are the same!")
        else:
            diff.save(diff_save_location)
    except ValueError as e:
        text = ("表示图片大小和box对应的宽度不一致,参考API说明:Pastes another image into this image."
                "The box argument is either a 2-tuple giving the upper left corner, a 4-tuple defining the left, upper, "
                "right, and lower pixel coordinate, or None (same as (0, 0)). If a 4-tuple is given, the size of the pasted "
                "image must match the size of the region.使用2纬的box避免上述问题")
        print("【{0}】{1}".format(e,text))

if __name__ == '__main__':
    compare_images('1.png',
                   '2.png',
                   '我们不一样.png')

执行结果:

 
 
 
 
 
 
 
 

第二种方法:

from PIL import Image
import math
import operator
from functools import reduce

def image_contrast(img1, img2):

    image1 = Image.open(img1)
    image2 = Image.open(img2)

    h1 = image1.histogram()
    h2 = image2.histogram()

    result = math.sqrt(reduce(operator.add,  list(map(lambda a,b: (a-b)**2, h1, h2)))/len(h1) )
    return result

if __name__ == '__main__':
    img1 = "./1.png"  # 指定图片路径
    img2 = "./2.png"
    result = image_contrast(img1,img2)
    print(result)

如果两张图片完全相等,则返回结果为浮点类型“0.0”,如果不相同则返回结果值越大。

同样用上面两张图片,执行结果为38,还是比较小的:

 
image

这样就可以在自动化测试用例中调用该方法来断言执行结果。

关于Pillow库的详细文档:

http://pillow.readthedocs.org/en/latest/index.html

用python对比两张图片的不同的更多相关文章

  1. python实战===用python对比两张图片的不同

    from PIL import Image from PIL import ImageChops def compare_images(path_one, path_two, diff_save_lo ...

  2. python 对比两个字典的差异

    实际遇到的问题逻辑很繁杂,就不全写了.最后是通过对比两个字典差异来解决的.找出两个字典的差异,可参考以下代码. dict1 = {'a':1,'b':2,'c':3,'d':4} dict2 = {' ...

  3. Python对比两个txt文件内容

    difflib模块作为python的标准库模块,无需安装,作用是比对文本之间的差异,且支持输出可读性比较强的html格式.#!coding=utf-8 # 2018-9-19 import sys i ...

  4. python对比两个文件问题

    写一个比较两个文本文件的程序. 如果不同, 给出第一个不同处的行号和 列号. 比较的时候可以使用zip()函数 a=open('test.txt','r') b=open('test2.txt','r ...

  5. python对比图片

    通过python的PIL模块可以对比两张图片是否相同,具体源码如下 from PIL import Image from PIL import ImageChops def compare_image ...

  6. python实现基于两张图片生成圆角图标效果的方法

    python实现基于两张图片生成圆角图标效果的方法 这篇文章主要介绍了python实现基于两张图片生成圆角图标效果的方法,实例分析了Python使用pil模块进行图片处理的技巧,分享给大家供大家参考. ...

  7. opencv_判断两张图片是否相同

    QQ:231469242 pip install opencv 如果找不到版本,去非官方下载opencv第三方包http://www.lfd.uci.edu/~gohlke/pythonlibs/ 下 ...

  8. 对比java和python对比

    对比java和python 对比java和python 2011年04月18日 1.难易度而言.python远远简单于java. 2.开发速度.Python远优于java 3.运行速度.java远优于 ...

  9. SQL Server对比两字段的相似度(函数算法)

    相似度函数 概述    比较两个字段的相似度    最近有人问到关于两个字段求相似度的函数,所以就写了一篇关于相似度的函数,分别是“简单的模糊匹配”,“顺序匹配”,“一对一位置匹配”.在平时的这种函数 ...

随机推荐

  1. mybatis源码之MapperMethod

    /** * @author Clinton Begin * @author Eduardo Macarron * @author Lasse Voss */ //这个类是整个代理机制的核心类,对Sql ...

  2. mac os 中如何修改顶栏图标的顺序

    很简单哦! 按住 cmd键同时鼠标选中那个图标,直接拖到你想要的位置即可.

  3. 0502-其他html标签

    其他html标签 一.框架 1.<frameset><frame/><frameset> 注意:html文档中不能有<body></body> ...

  4. 第1章-Struts2 概述 --- Struts2和MVC

    (一)Struts2和MVC的关系图: (1)控制器---FilterDispatcher 用户请求首先达到前段控制器(FilterDispatcher).FilterDispatcher负责根据用户 ...

  5. 三大框架:Struts+Hibernate+Spring

    三大框架:Struts+Hibernate+Spring Java三大框架主要用来做WEN应用. Struts主要负责表示层的显示 Spring利用它的IOC和AOP来处理控制业务(负责对数据库的操作 ...

  6. VS报错 error LNK2005: _DllMain@12 已经在 MSVCRTD.lib(dllmain.obj) 中定义

    链接报错: 错误 33 error LNK2005: _DllMain@12 已经在 MSVCRTD.lib(dllmain.obj) 中定义 E:\客户问题\w_王鹏\EventLibTest_Ti ...

  7. Android优秀github项目整理

    1.照相选相册,裁剪的 library TakePhotohttps://github.com/crazycodeboy/TakePhoto 2几行代码快速集成二维码扫描功能https://githu ...

  8. 绕过校园网WEB认证_iodine实现

    这篇文章是对我的上一篇文章"绕过校园网WEB认证_dns2tcp实现"的补充,在那篇文章中,我讲述了绕过校园网WEB认证的原理,并介绍了如何在windows系统下绕过校园网WEB认 ...

  9. C++负数取模

    预习: r=余数 a=被除数 b=除数 c=商 a/b=c........r r=a-(a/b)*b 一.下面的题目你能全做对吗?1.7/4=?2.7/(-4)=?3.7%4=?4.7%(-4)=?5 ...

  10. MyBatis打印SQL执行时间

    1.plugins MyBatis官网对于plugins的描述是这样的: MyBatis allows you to intercept calls to at certain points with ...