![](/img/trans.png)
[英]Python - Applying a color map to a grayscale numpy array and converting it to an image
[英]Applying a gradient map from image to another image with transparency in Python (PIL, numpy, etc)
我一直在努力嘗試將源自圖像文件的漸變 map 應用於包含 alpha/透明度的灰度圖像。 以下包括源圖像和漸變 map 圖像。 我首先嘗試使用 numpy 進行此操作,但沒有成功。 這樣做的目的是能夠使用漸變貼圖以編程方式為圖像層着色。 任何幫助,將不勝感激。 謝謝!
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')
這是結果。 它與您作為預期結果提供的示例不太匹配,但我認為這只是對輸入范圍應該是 map 到梯度的誤解。 我相信你可以自己解決這個問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.