5

Are there any nice ways to invert (not reverse, which is trivial, see Invert colormap in matplotlib) a colormap in matplotlib?

i.e., I want to take a png image, which has color tuples of the form (1,1,0,1) and use a matplotlib colormap to get an integer or floating point number out.

I think I can hack together something to do this, but I can't be the first to have wanted this functionality - does it exist somewhere I couldn't find, or is it built in to matplotlib?

Community
  • 1
  • 1
keflavich
  • 18,278
  • 20
  • 86
  • 118
  • 2
    This seems like a hard problem because there is no garuntee that the tuple will be a valid point in the color map, that is: there may be no `v` such that `cmap(v) -> (r,g,b,a)` so it is impossible to invert with out doing some huristics about 'close enough' – tacaswell Jan 21 '13 at 19:48
  • 1
    True. In my particular case, I have an image and a colorbar next to it, so in principle I could turn that colorbar into a map and be guaranteed of a 1-1 mapping. However, that's not trivial, nor is it general. So, a really good answer would have some method of dealing with 'close enough'. – keflavich Jan 21 '13 at 20:12
  • 1
    More specifics are needed to answer this question. There's no meaningful way to do this in the general case, where a 1-1 map doesn't exist or isn't somehow suggested, otherwise you'll end up asking questions like, "is blue or red closer to gray?" Therefore, a solution might exist, but only for a more specific problem, but we would need to know what that specific problem is. – tom10 Jan 22 '13 at 16:20
  • 2
    I just gave an answer to [this question](https://stackoverflow.com/questions/45177154/how-to-decode-color-mapping-in-matplotlibs-colormap). One would then need to apply this function to each pixel of the image. – ImportanceOfBeingErnest Jul 18 '17 at 22:35

0 Answers0