Python - Applying a color map to a grayscale array and converting it to an image

I want to achieve the gradient mapping effect available in Photoshop. There is already a message explaining the desired result. Also this answer covers exactly what I want to do, however

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

doesn't work for me, since I don't know how to normalize an array to 1.0 values.

Below is my code as I intend to work.

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)

Can someone point out possible options and an ideal way to apply a color map to this array? I do not want to conceive this, because there seems to be no easy way to convert the shape of a gun into an image.

You can also specify how to normalize the array, since I cannot do this and cannot find help anywhere.

+4
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

, , . . , .

+2

All Articles