%pylab inline
rcParams['figure.figsize'] = (10, 4) #wide graphs by default
from __future__ import print_function
from __future__ import division
from scipy.io import wavfile
sr,src = wavfile.read('20thFoxLol.wav') #read audio data
Populating the interactive namespace from numpy and matplotlib
src_mono = sum(src.astype(float)/src.shape[1], axis=1).astype(src.dtype)#pick one channel, convert to mono
from IPython.display import Audio
Audio(src_mono, rate=sr) #test play
minNumber = src_mono.min().astype(float)
maxNumberAfterUpShift = (src_mono - minNumber).max().astype(float) # maxNumner after shift src_mono up, let it start from 0
Processed = (((src_mono - minNumber)/maxNumberAfterUpShift)*255).astype(int) #scale src_mono to 0 - 255 range.
lengthOfArry = int(Processed.shape[0]) # find length of Processed
from PIL import Image
width = int(sqrt(lengthOfArry))+1
length = width
img = Image.new( 'RGB', (width,length), "black") # create a new black image
pixels = img.load() # create the pixel map
for i in range(img.size[0]): # for every colunm
for j in range(img.size[1]): # for every element
if (i*length+j)<lengthOfArry:
pixels[i,j] = (255-AfterProcessed[i*length+j],255-AfterProcessed[i*length+j], AfterProcessed[i*length+j]) # set the colour accordingly pixels[i,j] = (i, j, 100) # set the colour accordingly
# data of blue pixal is AfterProcessed itself
continue
else:
pixels[i,j] = (255,255,255) # if all data are outputed, fill pixal with (255,255,255)
continue
img.show()
img.save("img.bmp")
## The following code is converting the gerated image to Audio
#read bmp
imgRead = Image.open("img.bmp")
widthReceived,lengthReceived = imgRead.size
arrayReceived = zeros(widthReceived*lengthReceived)
for i in range(widthReceived): # for every colunm
for j in range(lengthReceived): # for every element
r, g, b = imgRead.getpixel((i, j)) # read each pixal
arrayReceived[i*length+j] = b # use values of blue pixals to recover audio data
if (r==255)and(g==255)and(b==255):
arrayReceived[i*length+j] = ((-minNumber)/maxNumberAfter)*255 # is pixal is (255,255,255), set the corresponding audio data as 0
continue
src_mono_recovered = ((arrayReceived/255)*maxNumberAfter+minNumber).astype(int) # recover audio data to its o
plot(src_mono_recovered) #plot read image
[<matplotlib.lines.Line2D at 0x570cf2b0>]
from IPython.display import Audio
Audio(src_mono_recovered, rate=sr) #Play results