繁体   English   中英

灰度热图到颜色梯度热图

[英]Grayscale Heatmap to Color Gradient Heatmap

我正在尝试采用一组 256x256px 8 位灰度 .png(具有透明度)并将灰度 .png 转换为相同大小的颜色 .png,但仍保持透明度。 我想使用的调色板是来自 R wesanderson包的 Zissou1,我已经进入了 Python 字典,其中每个键对应一个灰度值,每个值对应一个 HEX 颜色。

import os
from PIL import Image, ImageColor

### dic = the dictionary containing the palette in format {grayscale pixel value: "HEX COLOR"},
###       created earlier in the script

with Image.open("3.png") as im:
    fin = Image.new("RGBA", im.size)
    px = im.load()
    px1 = fin.load()
    for x in range(0,256):
        for y in range(0,256):
            px1.putpixel(x,y,ImageColor.getcolor(dic[px.getpixel(x,y)[1]],"RGBA"))
    fin.show()

我收到错误消息:

px1.putpixel(x,y,ImageColor.getcolor(dic[px.getpixel(x,y)[1]],"RGBA"))
AttributeError: 'PixelAccess' object has no attribute 'putpixel'

PIL 的PixelAccess.putpixel方法的第一个参数期望像素的坐标作为 (x,y) 元组传递:

px1.putpixel((x,y),ImageColor.getcolor(dic[px.getpixel(x,y)[1]],"RGBA"))

或者,考虑使用Image.point方法,该方法采用类似于您已经创建的查找表的查找表,以根据像素值映射图像。 有关更多详细信息,请参阅使用 PIL 中的 Image.point() 方法操作像素数据中的答案

扩展杰森的回答

PIL 给出的查找

使用Image.point(lookup_table, mode = 'L')您可以查找和转置图像的颜色。

lookup_table = ...
with Image.open("3.png") as orig:
    image = orig.point(lookup_table, mode = 'L')
    image.show()

要查看将Image.point方法与lookup_table一起使用的示例:

您自己的实现(通过改进命名修复)

或自己实现对your_dic的查找:

your_dic = ...
with Image.open("3.png") as orig:
    image = colored_from_map(orig, your_dic)
    image.show()

使用这个替代功能(你几乎做到了):

def colored_from_map(orig, map_to_color):
    image_in = orig.load()
    image = Image.new("RGBA", im.size)
    image_out = image.load()

    for x in range(0,256):
        for y in range(0,256):
            coords = (x,y)
            greyscale = image_in.getpixel(x,y)[1]
            color_name = map_to_color[greyscale]
            image_out.putpixel(coords, ImageColor.getcolor(color_name,"RGBA"))

    return image

保留 Alpha 通道(透明度)

请参阅ImageColor.getColor()方法体开头和结尾处的源代码

    color, alpha = getrgb(color), 255  # default to no-transparency
    if len(color) == 4:  # if your mapped color has 4th part as alpha-channel
        color, alpha = color[0:3], color[3]  # then use it

    # omitted lines
    else:
        if mode[-1] == "A":  # if the last char of `RGBA` is `A`
            return color + (alpha,)  # then return with added alpha-channel
    return color

(评论我的)

因此,您可以简单地将返回的颜色元组的第四个元素设置为原始灰度图像的前一个值:

            greyscale = image_in.getpixel(x,y)[1]  # containing original alpha-channel
            color_name = map_to_color[greyscale]  # name or hex
            mapped_color = ImageColor.getcolor(color_name,"RGB")  # removed the A
            transposed_color = mapped_color[:2] + (greyscale[3],)  # first 3 for RGB + original 4th for alpha-channel (transparency)
            image_out.putpixel(coords, transposed_color)

注意:因为 A(lpha-channel) 是从原始图像提供的,所以我从getColor调用的最后一个参数中删除了A 从技术上讲,您还可以从mapped_color[:2]中删除切片以mapped_color + (greyscale[3],)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM