0
所以我想淡出已经有透明背景的图像。使用Python PIL库垂直淡入透明背景图像到透明度
我在this question中发现了非透明图像的解决方案,但它不适用于具有透明背景的图像。那么我该怎么做才能将透明背景的图像垂直淡入淡出?
这里是我用来创建透明图像
bg = Image.new("RGBA", (width, height), (r,g,b,inputBgAlpha))
...
bg.paste(deviceBg, devicePadding, mask=deviceBg)
的代码和以下是我的尝试。它会产生一个带有颜色而不是透明的背景。
# https://stackoverflow.com/a/19236775/2603230
arr = numpy.array(bg)
alpha = arr[:, :, 3]
n = len(alpha)
alpha[:] = numpy.interp(numpy.arange(n), [0, 0.55*n, 0.05*n, n], [255, 255, 0, 0])[:,numpy.newaxis]
bg = Image.fromarray(arr, mode='RGBA')