-
-
Save endolith/fdd134b8c443e140641e0ab8e96e81bb to your computer and use it in GitHub Desktop.
Reverse colormap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice script - where are you from? I have positions available in Chicago