Because viridis, like all good colourmaps, is perceptually linear, it's easy to get the data from it: just use a greyscale version of the image. But you can rip the data from any pseudocolour image if you know (or can guess) the colourmap.
In the rip-data.py
example, here's the approach:
- Read the image and transform the values to the range 0-1.
- Guess the colourmap, in this case it's viridis. Matplotlib conveniently gives us the RGB triples that make up a colourmap.
- Make the KD tree and look up colours. This gives us a way to find the nearest RGB triple to every pixel in the image.
- Make a plot with a different colourmap.
In the rip-data-2.py
example, the colourmap is basically the hue wheel, so we can use the first channel from HSV to get at the data. The only snag is that there's hillshading, but fortunately the HS decomposition solves that too, putting the hillshade into the V channel.
In rip-data-3.py
, the problem is that the colourmap does not match the hue wheel... however it is included in the image. So we can provide its location (x = 120, y = 60 to 1700) and grab it to use as a look-up table.