下面是使用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绑定,并且具有用于图像合成/分段的一系列不错的功能。
在这种情况下,我们想像一下如何在图像处理程序中获得所需的输出:您将在原始图像上放置黑色(或其他颜色)阴影,并在其上粘贴第二个图像,但使用阈值(即像素相等或不同-所有中间值应四舍五入为“不同”)作为第二张图像的遮罩。
我修改了您的功能以创建这样的组合-
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')
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句