I have an 2D-array test_df
which is filled with zeroes and numbers:
0 1 2 3 4 5 6 7 ... 1272 1273 1274 1275 1276 1277 1278 1279
0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0
2 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0
3 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0
4 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0
I would like to create an image based on the array, which has the same dimensions as the array, and where each number would be represented by a particular color, and each pixel is colored in accordance to the number occupying the corresponding position of the array. So, if array's row 1 / column 2 is 0, and 0 = black, the corresponding pixel of the image (row 1 / column 2) is also black. I see it as a dictionary, where each number is mapped to a particular color (e.g. 0 : black), so that I can apply it to any other array and know exactly which color corresponds to which number. So far I was using this code to visualize the array, but it doesn't preserve the number - color mapping, and I'm wondering if there's a better way to create an image.
from matplotlib import pyplot as plt
im = Image.fromarray(np.uint8(cm.prism(test_df)*255))
im.show()
im.save(save_path)
Additionally, I'd like to use the same dictionary to decode an image back to the array which was used to build it.