将渐变图从一个图像应用到另一个具有 Python 透明度的图像(PIL、numpy 等)

Applying a gradient map from image to another image with transparency in Python (PIL, numpy, etc)

我一直在努力尝试将源自图像文件的渐变贴图应用于包含 alpha/transparency 的灰度图像。以下包括源图像和渐变贴图图像。我首先尝试使用 numpy,但没有成功。这样做的目的是能够使用渐变贴图以编程方式为图像层着色。任何帮助,将不胜感激。谢谢!

Sample Source Image

Sample Colormap / Gradient to Apply

Sample Intended Result

from PIL import Image

gray, alpha = Image.open(source_fn).convert('LA').split()
gradient = Image.open(gradient_fn).convert('RGB').resize((256, 1))
lut = []
for band in gradient.split():
    lut += list(band.getdata())

out = gray.point(lut, 'RGB')
out.putalpha(alpha)
out.save(out_fn, 'PNG')

这是结果。它与您作为预期结果提供的示例不太匹配,但我认为这只是对输入范围应该如何映射到梯度的误解。我相信你可以自己解决这个问题。