commit eae51478df570ae81ebfbf54ba28bb107f7109d5 Author: Pete Shadbolt Date: Wed Jan 14 20:29:22 2015 +0000 First commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d8dd753 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.wav diff --git a/README.mkd b/README.mkd new file mode 100644 index 0000000..25b33d0 --- /dev/null +++ b/README.mkd @@ -0,0 +1,11 @@ +1. Get an image + +![Image of a dog](test.jpg) + +2. `$ python spectrogram.py test.jpg` ... `Wrote test.jpg.wav` + +3. `$ sox test.jpg.wav -n spectrogram` + +![Spectrogram of a dog](spectrogram.png) + +Uses [SoX](http://sox.sourceforge.net/sox.html). diff --git a/run.sh b/run.sh new file mode 100755 index 0000000..a113fb0 --- /dev/null +++ b/run.sh @@ -0,0 +1,3 @@ +#!/bin/bash +python spectrogram.py test.jpg +sox test.jpg.wav -n highpass 200 gain -l -2 spectrogram diff --git a/spectrogram.png b/spectrogram.png new file mode 100644 index 0000000..f616170 Binary files /dev/null and b/spectrogram.png differ diff --git a/spectrogram.py b/spectrogram.py new file mode 100644 index 0000000..f0cc1f2 --- /dev/null +++ b/spectrogram.py @@ -0,0 +1,38 @@ +#!/usr/bin/python +import numpy as np +import matplotlib.image as mpimg +import wave +from array import array +from progressbar import ProgressBar + +def write_column(output_file, pixels, rescale=.1): + """ Write a single column of sound """ + curve = np.fft.ifft(pixels, len(pixels)*2).real + curve = np.array((curve-np.average(curve))*rescale, dtype=int) + data = array("h", curve).tostring() + output_file.writeframes(data) + + +def make_wav(image_filename): + """ Make a WAV file having a spectrogram resembling an image """ + image = mpimg.imread(image_filename) + image = np.sum(image, axis = 2).T[:, ::-1] + image = image**2 + + output_file = wave.open(image_filename+'.wav', 'w') + output_file.setparams((1, 2, 44100, 0, 'NONE', 'not compressed')) + + pb = ProgressBar().start() + n = float(len(image)) + for index, column in enumerate(image): + write_column(output_file, column) + pb.update(index*100/n) + + output_file.close() + print "Wrote %s.wav" % image_filename + + +if __name__ == '__main__': + import sys + make_wav(sys.argv[1]) + diff --git a/test.jpg b/test.jpg new file mode 100644 index 0000000..f4622c4 Binary files /dev/null and b/test.jpg differ