vIvId_gOat
vIvId_gOat

Reputation: 378

Is there a way to convert numbers to colours

I am doing a project on radio astronomy where I have to feed data to my PC from an input measuring radiation. The input will receive a higher value when pointed at a large source of radiation, like the Sun, and lower values when not. Values range from 0 to 9.

A sample input looks like:

1221211
1332331
1489841
1699971
1489841
1332331
1221211

The middle of the array which contains higher values should map to a brighter pixel, while lower values should map to a darker pixel correspondingly.

These numbers can be read using any python library and replaced with pixels with the corresponding values of numbers.

Upvotes: 1

Views: 93

Answers (1)

CDJB
CDJB

Reputation: 14516

You can use colourmaps from matplotlib for this - here is an example with your data:

Code:

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt

inpt = np.array([[1, 2, 2, 1, 2, 1, 1],
                 [1, 3, 3, 2, 3, 3, 1],
                 [1, 4, 8, 9, 8, 4, 1],
                 [1, 6, 9, 9, 9, 7, 1],
                 [1, 4, 8, 9, 8, 4, 1],
                 [1, 3, 3, 2, 3, 3, 1],
                 [1, 2, 2, 1, 2, 1, 1]])

norm = mpl.colors.Normalize(vmin=0, vmax=9)
plt.imshow(inpt, cmap='hot', norm=norm)

Output:

enter image description here

To just get a tuple of RGBA values, just use, for example:

>>> cmap = plt.cm.hot
>>> cmap(norm(5))
(0.0, 0.7110320290467349, 1.0, 1.0)
>>> mpl.colors.rgb2hex(cmap(norm(5))) # Hex Code
'#ff8000'

Upvotes: 2

Related Questions