混合多个图像,同时保留原始颜色
Blending multiple images while retaining the original colors
我想要的:
Done using photoshop (multiply)
blend() with alpha=0.5, also blending with the white image on which I pasted it
我的问题有什么解决方法吗?原图什么都可以,只是给个参考图
Image A with a transparent background (0,255,255,255)
Image B with a transparent bg (255,0,255,255)
还有其他方法可以做到这一点,但这应该是非常通用的并且适用于其他情况:
#!/usr/bin/env python3
from PIL import Image
import numpy as np
# Load both images and make into Numpy arrays
a=np.array(Image.open('A.png').convert('RGBA'))
b=np.array(Image.open('B.png').convert('RGBA'))
# Make masks of all opaque pixels in each image, i.e. alpha>0
mA = a[...,3] > 0
mB = b[...,3] > 0
# Make empty result image
res = np.zeros_like(a)
res[mA] = np.uint8([255,0,255,255]) # make all pixels from A magenta
res[mB] = np.uint8([0,255,255,255]) # make all pixels from B cyan
res[mA & mB] = np.uint8([0,0,255,255]) # make all pixels common to A and B blue
# Save result
Image.fromarray(res).show()
我想要的: Done using photoshop (multiply)
blend() with alpha=0.5, also blending with the white image on which I pasted it 我的问题有什么解决方法吗?原图什么都可以,只是给个参考图
Image A with a transparent background (0,255,255,255)
Image B with a transparent bg (255,0,255,255)
还有其他方法可以做到这一点,但这应该是非常通用的并且适用于其他情况:
#!/usr/bin/env python3
from PIL import Image
import numpy as np
# Load both images and make into Numpy arrays
a=np.array(Image.open('A.png').convert('RGBA'))
b=np.array(Image.open('B.png').convert('RGBA'))
# Make masks of all opaque pixels in each image, i.e. alpha>0
mA = a[...,3] > 0
mB = b[...,3] > 0
# Make empty result image
res = np.zeros_like(a)
res[mA] = np.uint8([255,0,255,255]) # make all pixels from A magenta
res[mB] = np.uint8([0,255,255,255]) # make all pixels from B cyan
res[mA & mB] = np.uint8([0,0,255,255]) # make all pixels common to A and B blue
# Save result
Image.fromarray(res).show()