瀏覽代碼

First commit

master
Pete Shadbolt 10 年之前
當前提交
eae51478df
共有 6 個檔案被更改,包括 53 行新增0 行删除
  1. +1
    -0
      .gitignore
  2. +11
    -0
      README.mkd
  3. +3
    -0
      run.sh
  4. 二進制
      spectrogram.png
  5. +38
    -0
      spectrogram.py
  6. 二進制
      test.jpg

+ 1
- 0
.gitignore 查看文件

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

+ 11
- 0
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).

+ 3
- 0
run.sh 查看文件

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

二進制
spectrogram.png 查看文件

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

+ 38
- 0
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])


二進制
test.jpg 查看文件

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

Loading…
取消
儲存