# Create a Colored Image from a Gray Image [Solved]

I am new here and I need some help.

I have a gray image, and I need to colour it using Python.

This is the kind of images I have:

And I need to transform it to be like the images that can be plot by using matplotlib ColorMap "CMRmap" like this one and save it:

Thank you in advance for helping me.

• answered 2019-07-18 16:19

Expanding on @Miki's comment, you simply need to use a colormap. The colored image shows the CMRmap colormap.

import cv2
import matplotlib.pyplot as plt

img = cv2.imread(r"path\to\img", 0)
plt.imshow(img, cmap='CMRmap')
plt.plot()


Output:

Matplotlib lists all the colormaps here

• answered 2019-07-19 10:09

Sounds like you've figured out the colormap part, but not the saving. Building on Shawn's answer, if you want to save the figure, make a call to plt.savefig() instead of plt.show(). Then pass the path you want to save it to as an argument.

import cv2
import matplotlib.pyplot as plt

img = cv2.imread(r"path\to\img", 0)
plt.imshow(img, cmap='CMRmap')
plt.savefig("\path\to\output\file")


Hope this helps!