# MAT 201A HW1: Produce a soundfile from image
# Jingxiang Liu
# jingxiang_liu@umail.ucsb.edu
MAT 201A HW1
Produce a soundfile from image
Jingxiang Liu
jingxiang_liu@umail.ucsb.edu
% pylab inline
from __future__ import print_function
from __future__ import division
Populating the interactive namespace from numpy and matplotlib
Load Image
img = imread('forest.jpg')
imshow(img)
<matplotlib.image.AxesImage at 0xc7df2e8>
Image Informations
img.dtype
dtype('uint8')
img.shape
(791L, 1024L, 3L)
Turn the image into a grayscale image, by forming a weighted sum of the R, G and B components, to help generating the audio signal.
img_gray = 0.2989 * img[:, :, 0] + 0.5870 * img[:, :, 1] + 0.1140 * img[:, :, 2]
imshow(img_gray, cmap = cm.gray)
<matplotlib.image.AxesImage at 0xc9b6978>
Generate a vector to present this image by realigning all the data from this image into a vector, in the order of from the left to the right for all the elements in each row and from the top to the bottom for all the rows.
sig = []
for k in range(len(img_gray[:,1])):
sig.extend(img_gray[k,:])
plot(sig)
[<matplotlib.lines.Line2D at 0x1aba9e10>]
Expend the range of the above signal so that it can be heard. Since the grayscale image has elements in the range [0, 256], shift the signal by 128 to make it be symmetric about 0.
sigout = (numpy.array(sig) - 128) * 200
plot(sigout)
[<matplotlib.lines.Line2D at 0x13b5d978>]
from scipy.io import wavfile
wavfile.write('outsig.wav', 44100, array(sigout, dtype=int16))
For some reason, the command aplay doesn't work, so I use another method to play the generated signal.
from IPython.display import Audio
Audio("outsig.wav")