Generate LUTs with Python

Warning: This post is several years old and the author has marked it as poor quality (compared to more recent posts). It has been left intact for historical reasons, but but its content (and code) may be inaccurate or poorly written.

I wrote a script to generate and display LUTs with Python. There has 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:

Blin_Glin_Rlin_scale

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!

Blin_Glin_Rlin.jpg (green)

ORIGINAL DATA:
(that’s the “flying W” and the FSK signal below it is WA5DJJ)

Blin_Glin_Rlin_graph
Linear colormap
Blin_Glin_Rlin
Linear colormap

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).

Blin_Glin_Rlin.jpg (blue)
BLUE CHANNEL: weak signals / noise floor
Blin_Glin_Rlin.jpg (red)
RED CHANNEL: strong signals / no noise
Blin_Glin_Rlin.jpg (green)
GREEN CHANNEL: original data!
Bsin_Glin_Rsin_graph
But what about nonlinear curves?
Bsin_Glin_Rsin
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
Nonlinear 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="IndextRedtGreentBluen"
for i in range(256):
    out+=("t%dt%dt%dt%dn"%(i,r[i],g[i],b[i]))
f.write(out)
f.close()