4

I am using matplotlib to convert arrays of 400 floats (read from a text file) to images of 100x100 pixels, using a pre-defined colormap:

vec = [ float(i) for i in l.split()[1:] ]
v = normalise(np.array(vec).reshape((20,20)))
im = Image.fromarray(np.uint8(cm.coolwarm(v)*255))
im = im.resize((100, 100))

This works fine. But now I'd like to reverse this process. That is, given an image, I'd like to go from the RGB values of each pixel back to scalars (to retrieve my original array). How would I do that?

NB: my question is related to this one, except that I don't want to take any image and convert it back to an array -- only the ones that have been created by my colormap in the first place.

linguist
  • 133
  • 1
  • 9
  • 1
    Since the colormapping is not necessarily a bijection, there is no general way to do this; also see comments on the other question. I would say it is possible to invert a colormapping of an image of which you know that is has been created from a *normalized* array with the coolwarm map (because that would be a bijection); however it's somehow cumbersome. The general advise would rather be: Never throw away your raw data, such that this kind of problem would never occur. – ImportanceOfBeingErnest Jul 13 '17 at 08:41
  • Thanks for the reply! So the reason I want to do this is that the image might be shared with someone who does not have access to the raw data and wants to retrieve it from the picture only. I'm afraid I would need to refresh my (small) knowledge of bijection, so I don't fully understand your comment on normalisation... but I am indeed normalising my floats in the range 0-1. Does that help me? Or are we talking about another type of normalisation? – linguist Jul 13 '17 at 09:46
  • 1
    Yes, the fact that you use the full range between 0 to 1 is one piece of information that you would need to be able to unambiguously invert the mapping. (It's not the acutal numbers, it's the information about the range that is important). Why don't you provide the raw data to the person you want to share your data with? It would be much easier for that person to create a plot with the colormapping of his choice from the data than to reverse engineer your mapping. – ImportanceOfBeingErnest Jul 13 '17 at 09:52
  • Yes, I understand. Well, this is supposed to be some cute visualisation of semantic vectors (i.e. each array expresses the meaning of a word / sentence / document / image, you name it). And the idea is that by looking at the image, you get back some very condensed meaning information about the original data (e.g. this was a webpage about programming in python, or an image with lions in). Same principle as a QR code, in a sense. I guess I could simply add the raw data as metadata in the image. This just seemed somewhat redundant. – linguist Jul 13 '17 at 10:08
  • 1
    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:34

0 Answers0