sghgw
sghgw

Reputation: 93

Create a grayscale image from a color coded source image in Python

I want to convert a color-coded image to grayscale. The colors in that image correspond to given values (e.g. black is 0 and red is 25) like on this color scale.

enter image description here

Is there a way to convert this image to grayscale and keep that color gradient, so that 0 on the scale remains black and 25 is shown as white?

I tried to convert it with matplotlib and also cv2 but ended up with grayscale images, that did not respect my given color gradient. I would appreciate an answer very much. Thank you!

Upvotes: 0

Views: 409

Answers (1)

user1196549
user1196549

Reputation:

Depending on the tools you use you can

  • read/convert the color-coded image as RGB,
  • convert RGB to grayscale

or

  • convert the colors in the gradient to grayscale,
  • read/convert the color-coded image using that grayscale palette.

The second approach is more efficient.


Update:

Upon reading your last comment (which should be in the original question), the options become

  • read/convert the color-coded image as RGB,
  • convert RGB to grayscale,
  • rescale by multiplying the pixel values by 10

or

  • convert the colors in the gradient to grayscale, rescaled to 0-255,
  • read/convert the color-coded image using that grayscale palette.

Upvotes: 1

Related Questions