python图片对比 并标红

关于Pillow库的详细文档:
https://pillow.readthedocs.io/en/stable/handbook/index.html

代码最终原地址:
https://www.blog.pythonlibrary.org/2016/10/11/how-to-create-a-diff-of-an-image-in-python/

此文章的原作者:
https://blog.csdn.net/weixin_45903952/article/details/104865694?utm_term=%E5%9B%BE%E7%89%87%E5%B7%AE%E5%BC%82%E5%AF%B9%E6%AF%94python&utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2allsobaiduweb~default-3-104865694&spm=3001.4430

代码:

from PIL import Image
from PIL import ImageChops

from screenshot_share import get_image

# get_image('http://cq.gov.cn','qqq1.png')

from PIL import Image
from PIL import ImageChops


# from PIL import ImageEnhance

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)
        # diff.show()            #不同的点图
        r, g, b, e = diff.split()  # RGB分离
        invertr = ImageChops.invert(r)  # 红色反向
        img1 = invertr.convert('1')  # 转成黑白图,黑点即红点

        img = img1.convert("RGBA")  # 转换格式,确保像素包含alpha通道
        width, height = img.size  # 长度和宽度
        for i in range(0, width):  # 遍历所有长度的点
            for j in range(0, height):  # 遍历所有宽度的点
                data = img.getpixel((i, j))  # 获取一个像素
                if (data.count(255) == 4):  # RGBA都是255,改成透明色
                    img.putpixel((i, j), (255, 255, 255, 0))
                else:
                    # if img.getpixel((i,j))[0]>200:
                    img.putpixel((i, j), (255, 0, 0, 255))
                    img.paste((255, 0, 0, 255), (i - 4, j - 4, i, j))  # 放大红点范围
        img = img.convert("RGB")
        image_one_l = image_one.convert("L")  # 转化为灰度
        image_one = image_one_l.convert("RGB")  # 再把灰度图像转为RGB
        # r,g,b=image_one.split()
        r, g, b = img.split()
        image = Image.composite(image_one, img, g)
        image.show()
        image.save("最终_" + diff_save_location)

        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('img.png',
                   'qqq1.png',
                   '我们不一样.png')

简单的对比图片差异

代码

im1 = Image.open('img.png')
im2 = Image.open('qqq1.png')
im3 = ImageChops.invert(im2)
Image.blend(im1,im3,0.5).show()

在这里插入图片描述
对比之后的图片在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值