Hi Matthias
AWESOME!!
This is a great example, thank you, it will come handy pretty soon, if you are going to share it officially, so I'd love to see the blog post about it.
In my current particular case, the problem is that I do not know the colormap of the original img to begin with. Someone from a slack group suggested to convert it to a paletted image format (PNG or GIF) using PIL, then grab the palette shared a solution involving quantization. I asked for their permission to share it in here, I'll keep you posted.
Back on your example of converting a bad colormap to a good one, I was working on adapting a Matlab tool by Peter Kovesi
http://peterkovesi.com/
projects/colourmaps/index.html
My idea would be to make it into a web app called 'rainbowbot' which would automatically detect bad colormaps either form online images or user uploaded images, and then provide them with tools to either equalize the colormaps or replace with a perceptual version with same hue range, or.....
It is in here
https://github.com/mycarta/
rainbowbot I am open to suggestions, and offers to collaborate.