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: enter image description here

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:

enter image description here

Thank you in advance for helping me.

2 answers

  • answered 2019-07-18 16:19 Shawn Mathew

    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:

    enter image description here

    Matplotlib lists all the colormaps here

  • answered 2019-07-19 10:09 Dominic McLoughlin

    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!