Python - 将颜色图应用于灰度 numpy 数组并将其转换为图像

Was*_*der 5 python numpy image-processing python-imaging-library

我想要实现 Photoshop 中提供的渐变映射效果。已经有一篇文章解释了期望的结果。另外,这个答案完全涵盖了我想做的事情,但是

im = Image.fromarray(np.uint8(cm.gist_earth(myarray)*255))
Run Code Online (Sandbox Code Playgroud)

不适合我,因为我不知道如何将数组标准化为 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)
Run Code Online (Sandbox Code Playgroud)

任何人都可以指出将颜色图应用于该数组的可能选项和理想方法吗?我不想绘制它,因为似乎没有一种简单的方法将 pyplot 图转换为图像。

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

jno*_*cho 2

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

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
Run Code Online (Sandbox Code Playgroud)

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

  • 谢谢!但对于这个问题,您还能建议一种应用颜色图的方法吗? (4认同)