Browse Source

First commit

master
Pete Shadbolt 5 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 @@
*.wav

+ 11
- 0
README.mkd View File

@@ -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).

+ 3
- 0
run.sh View File

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

BIN
spectrogram.png View File

Before After
Width: 944  |  Height: 591  |  Size: 194KB

+ 38
- 0
spectrogram.py View File

@@ -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])


BIN
test.jpg View File

Before After
Width: 960  |  Height: 717  |  Size: 115KB

Loading…
Cancel
Save