I want to apply a plt colormap to a medical grayscale image (14bit (16383 is the maximum pixel value) image stored as np.uint16) and save it as a single channel grayscale image. However when I do:
import matplotlib.pyplot as plt
import numpy as np
rand_img = np.random.randint(low=0, high=16383,size=(500,500),dtype=np.uint16)
# rand_img.shape = (500,500)
cm = plt.cm.gist_yarg
norm = plt.Normalize(vmin=0, vmax=((2**14)-1))
img = cm(norm(rand_img))
# img.shape = (500,500,4)
the resulting img.shape is a 4 channel (rgba?) image whereas what I want is a one channel image. The first 3 channels are all the same so I could just slice out one channel and use it. However the image turns out significantly darker as when I display it with, e.g.,
plt.imshow(rand_img, cmap=plt.cm.gist_yarg)
So how can I apply the colormap and save the image so that it looks exactly like when I use plt.imshow?
PS: When I use plt.imsave with the colormap the saved image looks as expected, however it is still stores as a 4 channel image and not as a single channel image.