0

I'm trying to read the colors of this palette into a numpy array:

enter image description here

First I get all the colours along a 1-pixel wide column:

  im_in = Image.open(inputfile)
  im_in = im_in.convert('RGBA').crop((50, 88, 50 + 1, 88 + 1049))

Then I convert the image into a numpy array and use the numpy.unique function to get unique columns.

  scale = np.array(im_in).squeeze()
  a, indx = np.unique(scale, axis=0, return_index=True)
  colors = scale[indx]

Now, the problem is that the order of the colours is not preserved:

  cm = LinearSegmentedColormap.from_list('asdf', np.array(colors) / 255, 
                                     N=len(colors))
  fig, ax = plt.subplots(figsize=(6, 1))
  fig.subplots_adjust(bottom=0.5)
  norm = mpl.colors.Normalize(vmin=5, vmax=10)
  fig.colorbar(mpl.cm.ScalarMappable(norm=norm, cmap=cm),
               cax=ax, orientation='horizontal', label='Some Units')
  fig.show()

And I get this shuffled array:

enter image description here

What am I doing wrong with the unique function?

Pythonist
  • 1,937
  • 1
  • 14
  • 25

0 Answers0