比较两个图像并突出显示第二个图像上的差异

Sum*_*man 8 image python-imaging-library

下面是python中使用PIL突出显示两个图像之间差异的当前工作代码.但其余的图像都是黑色的.

目前我想要显示背景以及突出显示的图像.

无论如何,我可以保持节目的背景更轻,只是突出差异.

from PIL import Image, ImageChops
point_table = ([0] + ([255] * 255))

def black_or_b(a, b):
    diff = ImageChops.difference(a, b)
    diff = diff.convert('L')
    # diff = diff.point(point_table)
    h,w=diff.size
    new = diff.convert('RGB')
    new.paste(b, mask=diff)
    return new

a = Image.open('i1.png')
b = Image.open('i2.png')
c = black_or_b(a, b)
c.save('diff.png')
Run Code Online (Sandbox Code Playgroud)

!https://drive.google.com/file/d/0BylgVQ7RN4ZhTUtUU1hmc1FUVlE/view?usp=sharing

jsb*_*eno 7

PIL确实有一些方便的图像处理方法,但是当想要开始进行严肃的图像处理时,还有很多缺点 -

大多数Python文档都会建议你切换到使用NumPy而不是你的像素数据,这将给你完全控制 - 其他成像库如leptonica,gegl和vips都有Python绑定和一系列很好的图像合成/分割功能.

在这种情况下,想象一下如何在图像处理程序中获得所需的输出:您将在原始图像上放置黑色(或其他颜色)阴影,并在此上粘贴第二个图像,但使用阈值(即像素相等或不同 - 所有中间值应四舍五入到"不同")作为第二图像的掩模.

我修改了你的函数来创建这样一个组合 -

from PIL import Image, ImageChops, ImageDraw
point_table = ([0] + ([255] * 255))

def new_gray(size, color):
    img = Image.new('L',size)
    dr = ImageDraw.Draw(img)
    dr.rectangle((0,0) + size, color)
    return img

def black_or_b(a, b, opacity=0.85):
    diff = ImageChops.difference(a, b)
    diff = diff.convert('L')
    # Hack: there is no threshold in PILL,
    # so we add the difference with itself to do
    # a poor man's thresholding of the mask: 
    #(the values for equal pixels-  0 - don't add up)
    thresholded_diff = diff
    for repeat in range(3):
        thresholded_diff  = ImageChops.add(thresholded_diff, thresholded_diff)
    h,w = size = diff.size
    mask = new_gray(size, int(255 * (opacity)))
    shade = new_gray(size, 0)
    new = a.copy()
    new.paste(shade, mask=mask)
    # To have the original image show partially
    # on the final result, simply put "diff" instead of thresholded_diff bellow
    new.paste(b, mask=thresholded_diff)
    return new


a = Image.open('a.png')
b = Image.open('b.png')
c = black_or_b(a, b)
c.save('c.png')
Run Code Online (Sandbox Code Playgroud)