Browse Source

First commit

Pete Shadbolt 4 years ago
commit
eae51478df
6 changed files with 53 additions and 0 deletions
  1. 1
    0
      .gitignore
  2. 11
    0
      README.mkd
  3. 3
    0
      run.sh
  4. BIN
      spectrogram.png
  5. 38
    0
      spectrogram.py
  6. BIN
      test.jpg

+ 1
- 0
.gitignore View File

@@ -0,0 +1 @@
1
+*.wav

+ 11
- 0
README.mkd View File

@@ -0,0 +1,11 @@
1
+1. Get an image
2
+
3
+![Image of a dog](test.jpg)
4
+
5
+2. `$ python spectrogram.py test.jpg` ... `Wrote test.jpg.wav`
6
+
7
+3. `$ sox test.jpg.wav -n spectrogram`
8
+
9
+![Spectrogram of a dog](spectrogram.png)
10
+
11
+Uses [SoX](http://sox.sourceforge.net/sox.html).

+ 3
- 0
run.sh View File

@@ -0,0 +1,3 @@
1
+#!/bin/bash
2
+python spectrogram.py test.jpg
3
+sox test.jpg.wav -n highpass 200 gain -l -2 spectrogram

BIN
spectrogram.png View File


+ 38
- 0
spectrogram.py View File

@@ -0,0 +1,38 @@
1
+#!/usr/bin/python
2
+import numpy as np
3
+import matplotlib.image as mpimg
4
+import wave
5
+from array import array
6
+from progressbar import ProgressBar
7
+
8
+def write_column(output_file, pixels, rescale=.1):
9
+    """ Write a single column of sound """
10
+    curve = np.fft.ifft(pixels, len(pixels)*2).real
11
+    curve = np.array((curve-np.average(curve))*rescale, dtype=int)
12
+    data = array("h", curve).tostring()
13
+    output_file.writeframes(data)
14
+
15
+
16
+def make_wav(image_filename):
17
+    """ Make a WAV file having a spectrogram resembling an image """
18
+    image = mpimg.imread(image_filename)
19
+    image = np.sum(image, axis = 2).T[:, ::-1]
20
+    image = image**2
21
+
22
+    output_file = wave.open(image_filename+'.wav', 'w')
23
+    output_file.setparams((1, 2, 44100, 0, 'NONE', 'not compressed'))
24
+
25
+    pb = ProgressBar().start()
26
+    n = float(len(image))
27
+    for index, column in enumerate(image):
28
+        write_column(output_file, column)
29
+        pb.update(index*100/n)
30
+
31
+    output_file.close()
32
+    print "Wrote %s.wav" % image_filename
33
+
34
+
35
+if __name__ == '__main__':
36
+    import sys
37
+    make_wav(sys.argv[1])
38
+

BIN
test.jpg View File