I am currently looking for at in which i can store a numpy.ndarray as an image, then save that image, and extract the pixel values of the image into a numpy.ndarray. The dimension of the numpy.ndarray with the pixel values should be the same as the numpy.ndarray what was used to create the plot.
I tried something like this:
def make_plot_store_data(name, data):
plt.figure()
librosa.display.specshow(data.T,sr=16000,x_axis='frames',y_axis='mel',hop_length=160,cmap=cm.jet)
plt.savefig(name+".png")
plt.close()
convert = plt.get_cmap(cm.jet)
numpy_output_interweawed = convert(data.T)
The first image looks like this:
The second image looks like this:
Why is this so messed up?


ndarraywould change shape just because it's rendered as an image. See my answer below for a working example.convert()maps scalars to RGBA, which will return an[N, N, 4]matrix, given an[N x N]input array. SeeColormapdocs for more.