0

Is there a way to set a colormap for sg.Image() or sg.DrawImage()? In my case I have a grayscale (single-band) thermal image that I'd like to show with a heat colormap. Short example of current code:

import PySimpleGUI as sg

layout = [[sg.Image(thermal_image_path, size=(600, 600))]]
window = sg.Window('Show image', size=(600, 600), 
                   resizable=True).Layout(layout).finalize()
user8188435
  • 191
  • 5
  • 14

1 Answers1

0

You could map the grey-tones to a range of different Hues and set the Saturation and Lightness constant - see Wikipedia article on HSL

#!/usr/bin/env python3

import numpy as np
import cv2

def heatmap(im):
    # Map range 0..255 of greys to Hues in range 60..180
    # Keep Lightness=127, Saturation=255
    # https://en.wikipedia.org/wiki/HSL_and_HSV#Hue_and_chroma
    H = (im.astype(np.float32) * 120./255.).astype(np.uint8) + 60
    L = np.full((h,w), 127, np.uint8)
    S = np.full((h,w), 255, np.uint8)
    HLS = cv2.merge((H,L,S))
    return cv2.cvtColor(HLS,cv2.COLOR_HLS2RGB)
    

# Create greyscale gradient
w, h = 256, 100
grey = np.repeat(np.arange(w,dtype=np.uint8).reshape(1,-1), h, axis=0) 
cv2.imwrite('grey.png',grey)     # debug only

# Apply heatmap to greyscale image
hm = heatmap(grey)

# Just for display
from PIL import Image
Image.fromarray(hm).save('result.png')

That makes the following greyscale image:

enter image description here

And then gets transformed to this:

enter image description here


Or you could shell out to ImageMagick with subprocess.run(), or use wand (its Python binding) to do this:

Make a 100x100 greyscale ramp - this is just setup to create an image to work with:

magick -size 100x100 gradient: grey.png

enter image description here

Make a 5-colour heatmap by varying the hues around the HSL circle - this only needs doing once and you can keep and reuse the image heat.png:

magick xc:"hsl(240,255,128)" xc:"hsl(180,255,128)" xc:"hsl(120,255,128))" xc:"hsl(60,255,128)" xc:"hsl(0,255,128)" +append heat.png

enter image description here

Map the shades of the greyscale image to our CLUT (colour lookup table) - this is the actual answer:

magick grey.png heat.png -clut result.png

enter image description here

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432