【问题标题】:Python - Applying a color map to a grayscale numpy array and converting it to an imagePython - 将颜色图应用于灰度 numpy 数组并将其转换为图像
【发布时间】:2015-04-07 00:04:58
【问题描述】:

我想实现 Photoshop 中可用的渐变映射效果。 There's already a post that explains the desired outcome. 另外,this answer 涵盖了我想要做的事情,但是

im = Image.fromarray(np.uint8(cm.gist_earth(myarray)*255))

不适合我,因为我不知道如何将数组标准化为 1.0 的值。

下面是我打算让它工作的代码。

im = Image.open(filename).convert('L') # Opening an Image as Grayscale
im_arr = numpy.asarray(im)             # Converting the image to an Array 

# TODO - Grayscale Color Mapping Operation on im_arr

im = Image.fromarray(im_arr)

谁能指出将颜色映射应用于此数组的可能选项和理想方式?我不想绘制它,因为似乎没有一种简单的方法可以将 pyplot 图形转换为图像。

另外,您能否指出如何规范化数组,因为我无法这样做并且在任何地方都找不到帮助。

【问题讨论】:

    标签: python image-processing numpy python-imaging-library


    【解决方案1】:

    要标准化图像,您可以使用以下过程:

    import numpy as np
    image = get_some_image() # your source data
    image = image.astype(np.float32) # convert to float
    image -= image.min() # ensure the minimal value is 0.0
    image /= image.max() # maximum value in image is now 1.0
    

    这个想法是首先移动图像,所以最小值为零。这也将处理负最小值。然后将图像除以最大值,得到的最大值为 1。

    【讨论】:

    • 谢谢!但是对于这个问题,您能否也建议一种应用颜色图的方法?
    • 您发布的代码不适用于我建议的添加规范化?
    • 确实如此。我只是想知道是否有一种方法可以在功能上应用自定义颜色图。
    • @AndreyShipilov - 是的,我做到了。 jnovacho 的解决方案有所帮助,但我不得不即兴发挥。如果您遇到问题,请联系我,我非常乐意提供帮助。
    • @WasifHyder 谢谢你。我实际上是用opencv做到的。这显然很容易。现在我有点不知道如何将 opencv 数据保存到 Django 图像字段中。有什么想法吗?
    猜你喜欢
    • 1970-01-01
    • 2021-04-02
    • 1970-01-01
    • 2018-12-19
    • 1970-01-01
    • 2020-12-19
    • 2021-11-24
    • 2011-12-28
    • 1970-01-01
    相关资源
    最近更新 更多