2015-04-07 9 views
4

我想要實現Photoshop中可用的漸變映射效果。 There's already a post that explains the desired outcome.此外,this answer佔地面積正是我想做的事,但Python - 將彩色貼圖應用於灰度numpy陣列並將其轉換爲圖像

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數字轉換爲圖像。

另外,你能指出如何正常化數組,因爲我無法這樣做,並且無法在任何地方找到幫助。

回答

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 

的想法是首先轉移的形象,因此最小值是零。這也會照顧負面的最小值。然後用最大值對圖像進行分割,所得到的最大值爲1。

+1

謝謝!但對於這個問題,你是否也可以建議一種應用顏色圖的方法? –

+0

您發佈的代碼不適用於我建議的添加規範化? – jnovacho

+0

它的確如此。我只想知道是否有辦法在功能上應用自定義顏色映射。 –