Generate LUTs with Python
1,008 words | Posted on May 10th, 2010
Scott was 24.63 years old when he wrote this!
Filed under: General, Python, Radio
Everyone’s favorite slacker hacked together a script to not only generate, but also thoroughly document equation-based LUTs (look up tables) with Python. There’s been a lot of heated discussion in the QRSS Knights mailing list as to the use of color maps when representing QRSS data. I’ll make a separate post (perhaps later?) documenting why it’s so critical to use particular mathematically-generated color maps rather than empirical “looks good to me” color selections. Anyway, this is what I came up with:
For my QRSS needs, I desire a colormap which is aesthetically pleasing but can also be quickly reverted to its original (gray-scale) data. I accomplished this by choosing a channel (green in this case) and applying its intensity linearly with respect to the value it represents. Thus, any “final” image can be imported into an editor, split by RGB, and the green channel represents the original data. This allows adjustment of contrast/brightness and even the reassignment of a different colormap, all without losing any data!
ORIGINAL DATA:
(that’s the “flying W” and the FSK signal below it is WA5DJJ)
Note that it looks nice, shows weak signals, doesn’t get blown-out by strong signals, and it fully includes the noise floor (utilizing all available data).
BLUE CHANNEL: weak signals / noise floor
RED CHANNEL: strong signals / no noise
GREEN CHANNEL: original data!!!

They look okay, but not as great as linear
DOWNLOAD LUTs
The following links are downloadable LUTs which can be applied to 8-bit grayscale images using most editors (i.e., MBF ImageJ) generated by the python script below.
Linear LUT
Sinusoidal LUT
This is the Python script I wrote to generate the downloadable LUTs, graphs, and scale bars / keys / legends which are not posted. It requires python, matplotlib, and PIL.
import math
import pylab
from PIL import Image
####################### GENERATE RGB VALUES #######################
r,g,b=[],[],[]
name="Blin_Glin_Rlin"
for i in range(256):
if i>128: #LOW HALF
j=128
k=i
else: #HIGH HALF
k=128
j=i
#b.append((math.sin(3.1415926535*j/128.0/2))*256)
#r.append((1+math.sin(3.1415926535*(k-128*2)/128.0/2))*256)
r.append(k*2-255)
g.append(i)
b.append(j*2-1)
if r[-1]<0:r[-1]=0
if g[-1]<0:g[-1]=0
if b[-1]<0:b[-1]=0
if r[-1]>255:b[-1]=255
if g[-1]>255:g[-1]=255
if b[-1]>255:b[-1]=255
####################### SAVE LUT FILE #######################
im = Image.new("RGB",(256*2,10*4))
pix = im.load()
for x in range(256):
for y in range(10):
pix[x,y] = (r[x],g[x],b[x])
pix[x,y+10] = (r[x],0,0)
pix[x,y+20] = (0,g[x],0)
pix[x,y+30] = (0,0,b[x])
a=(g[x]+g[x]+g[x])/3
pix[256+x,y] = (a,a,a)
pix[256+x,y+10] = (r[x],r[x],r[x])
pix[256+x,y+20] = (g[x],g[x],g[x])
pix[256+x,y+30] = (b[x],b[x],b[x])
#im=im.resize((256/2,40),Image.ANTIALIAS)
im.save(name+"_scale.png")
####################### PLOT IT #######################
pylab.figure(figsize=(8,4))
pylab.grid(alpha=.3)
pylab.title(name)
pylab.xlabel("Data Value")
pylab.ylabel("Color Intensity")
pylab.plot(g,'g-')
pylab.plot(r,'r-')
pylab.plot(b,'b-')
pylab.axis([-10,266,-10,266])
pylab.subplots_adjust(top=0.90, bottom=0.14, left=0.1, right=0.97)
pylab.savefig(name+"_graph.png",dpi=60)
#pylab.show()
####################### SAVE LUT FILE #######################
f=open(name+".lut",'w')
out="Index\tRed\tGreen\tBlue\n"
for i in range(256):
out+=("\t%d\t%d\t%d\t%d\n"%(i,r[i],g[i],b[i]))
f.write(out)
f.close()
This entry was posted on Monday, May 10th, 2010 at 10:31 pmand is filed under General, Python, Radio. You can follow any responses to this entry through the RSS 2.0 feed. You can skip to the end and leave a response. Pinging is currently not allowed.


