我一直在尝试将图像(在python中使用PIL)混合在一起形成一个组合图像。但是,为了使两个图像正确混合,我一直将alpha保持为0.5。这导致图像半透明并失去其不透明度。无论如何,我可以混合2张图像而不会失去它的不透明度。 守则:
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
答案 0 :(得分: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()