4

I want to achieve the gradient mapping effect that's available in Photoshop. There's already a post that explains the desired outcome. Also, this answer covers exactly what I want to do, however

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

is not working for me since I don't know how to normalize the array to values of 1.0.

Below is my code as I intend for it 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 anyone indicate the possible options and ideal way to apply a color map to this array? I don't want to plot it as there doesn't seem to be a simple way to convert a pyplot figure to an image.

Also, can you point out how to normalize the array since I'm unable to do so and can't find help anywhere.

Community
  • 1
  • 1
Wasif Hyder
  • 1,089
  • 1
  • 9
  • 13

1 Answers1

2

To normalize the image you can use following procedure:

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

The idea is to first shift the image, so the minimal value is zero. This will take care of negative minimum as well. Then you divide the image by the maximum value, so the resulting maximum is one.

jnovacho
  • 2,635
  • 5
  • 21
  • 41
  • 3
    Thanks! But for this question, can you also suggest a way to apply the color map? – Wasif Hyder Apr 08 '15 at 21:54
  • The code you posted doesn't work with the added normalization I suggested? – jnovacho Apr 09 '15 at 12:19
  • It does. I'm only wondering if there's a way to functionally apply a custom color map. – Wasif Hyder Apr 12 '15 at 14:33
  • @AndreyShipilov - Yeah I did. jnovacho's solution helped but I had to improvise a bit. If you are having trouble, just hit me up and I'd be more than happy to help. – Wasif Hyder Jun 03 '17 at 12:46
  • @WasifHyder thanks man. I actually did it with opencv. It's pretty easy apparently. Now I'm a bit stuck how to save the opencv data into Django image field. Any idea? – Andrey Shipilov Jun 03 '17 at 12:47
  • @AndreyShipilov That's django specific so I can't comment - but you'll be able to find a question for it. Just look into the exporting options on opencv, and find which format django will support. – Wasif Hyder Jun 04 '17 at 17:14