Skip to content

Instantly share code, notes, and snippets.

@endolith
Forked from stefanv/reverse_cmap.py
Last active July 26, 2017 18:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save endolith/fdd134b8c443e140641e0ab8e96e81bb to your computer and use it in GitHub Desktop.
Save endolith/fdd134b8c443e140641e0ab8e96e81bb to your computer and use it in GitHub Desktop.
Reverse colormap
from skimage import io as sio
import skimage
import numpy as np
import matplotlib.pyplot as plt
filename = 'Lennert jet.png'
img = skimage.img_as_float(sio.imread(filename))[:,:,:3]
jet = plt.cm.jet
jet._init()
lut = jet._lut[..., :3]
z = img - lut[:, None, None, :]
z *= z
d = z.sum(axis=-1)
out = d.argmin(axis=0)
"""
Create new image, keeping colors that aren't in the old colormap
"""
plt.imshow(d.min(axis=0))
new_cmap = plt.cm.get_cmap('bone')
out_img = img.copy()
#matches = d.min(axis=0) < 0.005
# Exclude white
matches = np.ones(img.shape[:2], dtype=bool)
matches[np.amin(img, axis=-1) > 0.7] = False
# Exclude black
matches[np.amax(img, axis=-1) < 0.4] = False
#imshow(matches)
out_img[matches] = new_cmap(out[matches])[:, :3]
plt.imsave(f'Reversed {filename}', out_img)
#f, (ax0, ax1, ax2) = plt.subplots(1, 3)
#ax0.imshow(img, cmap=plt.cm.gray)
#ax1.imshow(out, cmap=plt.cm.gray)
#ax2.imshow(out, cmap=plt.cm.jet)
#plt.show()
Copy link

ghost commented Jul 26, 2017

Nice script - where are you from? I have positions available in Chicago

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment