%pylab inline
from __future__ import print_function
from __future__ import division
from scipy.io import wavfile
Populating the interactive namespace from numpy and matplotlib
# load images
img1 = imread('campbell1.jpg')
img2 = imread('campbell2.jpg')
img3 = imread('cover.jpg')
img4 = imread('patti.jpg')
img5 = imread('pink.jpg')
img6 = imread('reed.jpg')
img7 = imread('sea.jpg')
img8 = imread('square.jpg')
img9 = imread('mao.jpg')
img10 = imread('chair.jpg')
start from the top-left of the image
read each pixel's R, G, B value serially by column
put them into a list and write into sound wave
( list as [R,G,B,R,G,B,R,G,B...] )
imshow(img1)
img1.shape
(480, 600, 3)
# set initial value of the output signal
pixelValue = img1[0,0,:].astype(int16)
outputSignal = list((pixelValue - 100) * 200)
outputSignal
[-9800, -200, -8200]
# loop in rows and columns
# limit the i in range(0,100) to speed up the program
for i in range(0,100):
for j in range(1,img1.shape[0]):
pixelValue = img1[j,i,:].astype(int16)
outputSignal = outputSignal + list((pixelValue -100) * 200)
plot(outputSignal)
[<matplotlib.lines.Line2D at 0x10b1d9b90>]
wavfile.write('pisic_0.wav', 44100, array(outputSignal, dtype=int16))
from IPython.display import Audio
Audio(outputSignal,rate=44100)
# take a look at the detail of the wave
plot(outputSignal)
x=600*200
xlim((x+0, x+2400))
(120000, 122400)
apply Procedure 1 to another image:¶
imshow(img2)
img2.shape
(300, 600, 3)
pixelValue = img2[0,0,:].astype(int16)
outputSignal = list((pixelValue - 100) * 200)
for i in range(0,100):
for j in range(1,img2.shape[0]):
pixelValue = img2[j,i,:].astype(int16)
outputSignal = outputSignal + list((pixelValue -100) * 200)
plot(outputSignal)
[<matplotlib.lines.Line2D at 0x10b1e64d0>]
wavfile.write('pisic_1.wav', 44100, array(outputSignal, dtype=int16))
from IPython.display import Audio
Audio(outputSignal,rate=44100)
from Procedure 1:¶
As the R,G,B value of each pixel are usually different for colorful image, the values in the list vary fast.
And as it loops a lot, this procedure is really slow.
Different from Procedure 1:
seperate the R, G, B data into 3 parts
read each pixel's R value serially by column
after finishing the whole image
then read G value and then read B value
( list as [R,R,R...G,G,G...B,B,B...] )
imshow(img1)
img1.shape
(480, 600, 3)
pixelValue = img1[0,0,:].astype(int16)
outputSignal = list((pixelValue - 100) * 200)
# loop in R, G, B
for i in range(0,3):
# loop in rows
for j in range(1,img1.shape[0]):
pixelValue = img1[j,:,i].astype(int16)
outputSignal = outputSignal + list((pixelValue - 100) * 200)
plot(outputSignal)
[<matplotlib.lines.Line2D at 0x10d24d9d0>]
# take a look at the detail of the wave
# shape like a square wave
plot(outputSignal)
x=600*200
xlim((x+0, x+2400))
(120000, 122400)
# This is the most beautiful one I've ever generated.
# It sounds like a prelude of post rock musics.
# It has some sort of tune, which seperates itself from the white-noise-like-sound before.
wavfile.write('pisic_2.wav', 44100, array(outputSignal, dtype=int16))
from IPython.display import Audio
Audio(outputSignal,rate=44100)
apply Procedure 2 to another image:¶
imshow(img3)
img3.shape
(500, 500, 3)
pixelValue = img3[0,0,:].astype(int16)
outputSignal = list((pixelValue - 100) * 200)
for i in range(0,3):
for j in range(1,img3.shape[0]):
pixelValue = img3[j,:,i].astype(int16)
outputSignal = outputSignal + list((pixelValue - 100) * 200)
plot(outputSignal)
[<matplotlib.lines.Line2D at 0x10cf0b1d0>]
wavfile.write('pisic_3.wav', 44100, array(outputSignal, dtype=int16))
from IPython.display import Audio
Audio(outputSignal,rate=44100)
from Procedure 2:¶
By separating the R,G,B value, the values in the list are more relevant to the pattern of the image.
For most images, the values in the list vary in a slower pace than in procedure 1 and produce sound with relatively low vibration.
However, for both Procedure 1 and 2, as the frequency of the variation of R, G, B value are low to most images, the sounds produced are very flat and toneless to hear.
cut the image into N * N pieces
loop for N * N times, and in each time
generate a random number to choose which piece to be read
read each piece's value with Procedure 2
imshow(img1)
img1.shape
(480, 600, 3)
from random import randint
block = 60
pixelValue = img1[0,0,:].astype(int16)
outputSignal = list((pixelValue - 100) * 200)
# loop in 8 * 10 pieces
for a in range(0,8):
for b in range(0,10):
x = randint(0,7)
y = randint(0,9)
# loop in each piece
for i in range(0,3):
for j in range(x*block,(x+1)*block):
pixelValue = img1[j,y*block:(y+1)*block,i].astype(int16)
outputSignal = outputSignal + list((pixelValue - 100) * 200)
plot(outputSignal)
[<matplotlib.lines.Line2D at 0x10f6a5bd0>]
from IPython.display import Audio
Audio(outputSignal,rate=44100)
# This is my second favorite. The random choosing adds more variety to the sound.
wavfile.write('pisic_4.wav', 44100, array(outputSignal, dtype=int16))
apply Procedure 3 to another image:¶
imshow(img3)
img3.shape
(500, 500, 3)
from random import randint
block = 50
pixelValue = img3[0,0,:].astype(int16)
outputSignal = list((pixelValue - 100) * 200)
for a in range(0,10):
for b in range(0,10):
x = randint(0,9)
y = randint(0,9)
for i in range(0,3):
for j in range(x*block,(x+1)*block):
pixelValue = img3[j,y*block:(y+1)*block,i].astype(int16)
outputSignal = outputSignal + list((pixelValue - 100) * 200)
plot(outputSignal)
[<matplotlib.lines.Line2D at 0x1108e2d10>]
from IPython.display import Audio
Audio(outputSignal,rate=44100)
wavfile.write('pisic_5.wav', 44100, array(outputSignal, dtype=int16))
from Procedure 3:¶
By cutting the image into pieces and randomly choosing one to read, the signals produced gain more variety. And the sounds have more changes in tune.
However, there are more high-frequency-noise-like-sound mixed in the sound. I will try some hand-made low-pass filter later.