Reputation: 19
I am trying to visualize music into an image by using sounddevice to input the sound and then converting it to a numpy array. The array is 2D and so I convert it to 3D (otherwise I only get a single thin vertical line in the image). However when I use PIL to show the image it says 'Cannot handle this datatype' The code is mentioned below:
import sounddevice as sd
from scipy.io.wavfile import write
import soundfile as sf
import numpy
from numpy import zeros, newaxis
from PIL import Image
fs = 44100 # Sample rate
seconds = 3 # Duration of recording
myrecording = sd.rec(int(seconds * fs), samplerate=fs, channels=2)
sd.wait() # Wait until recording is finished
print(myrecording)
print(numpy.shape(myrecording))
write('output.wav', fs, myrecording) # Save as WAV file
filename = 'output.wav'
A=myrecording[:,:,newaxis]
print(A)
im = Image.fromarray((A * 255).astype(numpy.uint8))
im.show()
I expect to get an image which shows colours corresponding to the sound being inputted in
Upvotes: 0
Views: 133
Reputation: 726
This depends a little bit on what you want to do.
You have two channels with n-samples ((nsamples, 2) ndarray
); do you want each channel to be a column of the image where the color varies depending on what the value is? That is why you were getting a very narrow image when you just plot myrecording
.
You do not really have the data to create a full 2D image, unless you reshape the time series data to be something more like a square (so it actually looks like an image), but then you sort of lose the time dependence nature that I think you are going for.
Upvotes: 0