Hide images in sound
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
1.1KB

  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. def write_column(output_file, pixels, rescale=.1):
  8. """ Write a single column of sound """
  9. curve = np.fft.ifft(pixels, len(pixels)*2).real
  10. curve = np.array((curve-np.average(curve))*rescale, dtype=int)
  11. data = array("h", curve).tostring()
  12. output_file.writeframes(data)
  13. def make_wav(image_filename):
  14. """ Make a WAV file having a spectrogram resembling an image """
  15. image = mpimg.imread(image_filename)
  16. image = np.sum(image, axis = 2).T[:, ::-1]
  17. image = image**2
  18. output_file = wave.open(image_filename+".wav", "w")
  19. output_file.setparams((1, 2, 44100, 0, "NONE", "not compressed"))
  20. pb = ProgressBar().start()
  21. n = float(len(image))
  22. for index, column in enumerate(image):
  23. write_column(output_file, column)
  24. pb.update(index*100/n)
  25. output_file.close()
  26. print "Wrote %s.wav" % image_filename
  27. if __name__ == "__main__":
  28. import sys
  29. make_wav(sys.argv[1])