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

目前,我想显示背景以及突出显示的图像。

无论如何,我可以使节目的背景保持更浅,仅突出显示差异。

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')

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

最佳答案

PIL确实有一些方便的图像处理方法,
但也有很多缺点
开始进行认真的图像处理-

大多数Python文学建议您切换
在您的像素数据上使用NumPy,这将给
您完全掌控-
其他成像库,例如leptonica,gegl和vips
都具有Python绑定(bind)和一系列不错的功能
用于图像合成/分割。

在这种情况下,要想像的是
在图像处理程序中获得所需的输出:
您将需要使用黑色(或其他颜色)的阴影来放置
原始图片,然后粘贴第二张图片,
但使用阈值(即像素等于或等于
是不同的-所有中间值都应四舍五入
将“差异”作为第二图像的 mask 。

我修改了您的功能以创建这样的组合-

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')

关于image - 比较两个图像并突出显示第二个图像上的差异,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30277447/

10-11 22:16
查看更多