ソースを参照

First commit

master
Pete Shadbolt 9年前
コミット
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 ファイルの表示

変更前 変更後
幅: 944  |  高さ: 591  |  サイズ: 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 ファイルの表示

変更前 変更後
幅: 960  |  高さ: 717  |  サイズ: 115KB

読み込み中…
キャンセル
保存