1

我一直在尝试将图像混合在一起(在 python 中使用 PIL)以形成一个组合图像。但是,为了使两张图像正确融合,我一直将 alpha 保持为 0.5。这导致图像是半透明的并失去其不透明度。无论如何我可以在不失去不透明度的情况下混合这两个图像。编码:

import Image
a = Image.open("image1.png")
b = Image.open("image2.png")
c = Image.blend(a,b,0.5)
c.save("abc.png", "PNG")

这些是图像:(我是新用户,不允许发布图像或超过 2 个超链接。因此,我正在放置链接。)

image1 - http://i.stack.imgur.com/EphiR.png

abc - http://i.stack.imgur.com/useql.png

4

1 回答 1

0

这是一个可以改进的快速而肮脏的版本。

from PIL import Image
from PIL import ImageChops
from PIL import ImageOps
import numpy as np

a = Image.open("a.png")
b = Image.open("b.png")

# Finds the difference between the two images. Then inverts so differences are black.
diff = ImageOps.invert(ImageChops.difference(a, b)) 
# Then we use numpy to convert black to red. 
image = diff.convert('RGBA')
data = np.array(image)

r1,g1,b1 = 0,0,0
r2,g2,b2 = 255,0,0

red, green, blue = data[:,:,0], data[:,:,1], data[:,:,2]
mask = (red == r1) & (green == g1) & (blue == b1)
data[:,:,:3][mask] = [r2, g2, b2]
# Then we convert the array back to an image.
overlay = Image.fromarray(data)
# And blend with the original. 
imgblend = ImageChops.add(a.convert('RGBA'), overlay,2)
imgblend.show()
于 2013-08-20T18:12:54.480 回答