I have a Grayscale 'TIF' image which I've read as a numpy array which is 2D
The pixel intensities ranging from 17 to 317 in my 2D array.
I have two challenges
convert this 2D array into RGB image
scale the pixel intensities (between 17 to 317) to RGB values and show the Gray scale image as RGB color image
I unfortunately cannot use Opencv and Matplotlib as the end product on which I am working does not support these
It seems to me that there are 3 aspects to your question:
matplotlib
.So, to create a colormap, we need a list of 256 RGB values in which
we can lookup any greyscale value and find the colour we want to show it as. I understand you can't use matplotlib
in production, but you can grab a colormap or two from there on some other machine and copy it to your production machine. So, let's do that. We'll take the viridis
colormap:
#!/usr/bin/env python3
import numpy as np
from matplotlib import cm
# Get 256 entries from "viridis" or any other Matplotlib colormap
colmap = cm.get_cmap('viridis', 256)
# Make a Numpy array of the 256 RGB values
# Each line corresponds to an RGB colour for a greyscale level
np.savetxt('cmap.csv', (colmap.colors[...,0:3]*255).astype(np.uint8), fmt='%d', delimiter=',')
If we look at that file "cmap.csv"
, it has 256 lines and starts like this:
68,1,84
68,2,85
68,3,87
69,5,88
69,6,90
69,8,91
70,9,92
...
...
That means anywhere we see 0
in the greyscale image, we actually colour the pixel rgb(68,1,86)
. Anywhere we see 1
in the greyscale it maps to rgb(68,2,85)
. Presumably you can copy that file to your production machine and you can choose any one of Matplotlib
s colormaps.
Regarding scaling, you have posted an 8-bit PNG image with a range of 0..117 rather than a 16-bit TIFF image with a range of 17..317, so that is not most helpful. Also, you have not said how you plan to read a TIFF on a system that doesn't have OpenCV or matplotlib, so I don't know whether you have PIL/Pillow or plan to use tifffile.
Instead then, I will create a dummy 32x32 image with a range of 17..317 like this:
grey = np.random.randint(17,318, (32,32))
That looks like this:
array([[244, 75, 237, ..., 154, 190, 70],
[286, 247, 158, ..., 150, 267, 124],
[170, 305, 237, ..., 126, 111, 236],
...,
[163, 292, 184, ..., 24, 253, 177],
[314, 34, 36, ..., 87, 316, 182],
[258, 153, 278, ..., 189, 57, 196]])
If we now want to scale that from the range 17..317 into the range 0..255, we can use:
rescaled = ((grey.astype(float) - grey.min())*255/(grey.max() - grey.min())).astype(np.uint8)
which gives us this:
array([[192, 49, 187, ..., 116, 147, 45],
[228, 195, 119, ..., 113, 212, 90],
[130, 244, 187, ..., 92, 79, 186],
...,
[124, 233, 141, ..., 5, 200, 136],
[252, 14, 16, ..., 59, 254, 140],
[204, 115, 221, ..., 146, 34, 152]], dtype=uint8)
Looking up can be done by loading the CSV file containing our colourmap, and taking the corresponding elements from the colormap as indexed by your greyscale values in the range 0...255:
#!/usr/bin/env python3
import numpy as np
from PIL import Image
# Load image as greyscale and make into Numpy array
grey = np.array(Image.open('TdoGc.png').convert('L'))
# Load RGB LUT from CSV file
lut = np.loadtxt('cmap.csv', dtype=np.uint8, delimiter=',')
# Make output image, same height and width as grey image, but 3-channel RGB
result = np.zeros((*grey.shape,3), dtype=np.uint8)
# Take entries from RGB LUT according to greyscale values in image
np.take(lut, grey, axis=0, out=result)
# Save result
Image.fromarray(result).save('result.png')
If you scale your greyscale image to the full range after reading, using this line:
grey = ((grey.astype(float) - grey.min())*255/(grey.max() - grey.min())).astype(np.uint8)
You will get this:
If you want to visualise your colormap, change the line above that looks like this:
grey = np.array(Image.open('TdoGc.png').convert('L'))
into this so that it generates a gradient (ramp) image:
grey = np.repeat(np.arange(256,dtype=np.uint8).reshape(1,-1), 100, axis=0)
Then you can see your colourmap:
A quick hack to handle segmented linear colormaps that don't have a class variable enumerating the colours. So you can make the cmap.csv
file from the autumn
colourmap like this:
import numpy as np
from matplotlib import cm
# Get "autumn" colourmap
colmap = cm.get_cmap('autumn')
# Save 256 RGB entries as CSV - one for each of grey levels 0..255
np.savetxt('cmap.csv', np.array([colmap(i/255)[:3] for i in range(256)]) * 255, fmt="%d", delimiter=',')