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.

34 lines
907B

  1. #!/usr/bin/python
  2. import numpy as np
  3. import matplotlib.image as mpimg
  4. import wave
  5. from array import array
  6. def make_wav(image_filename):
  7. """ Make a WAV file having a spectrogram resembling an image """
  8. # Load image
  9. image = mpimg.imread(image_filename)
  10. image = np.sum(image, axis = 2).T[:, ::-1]
  11. image = image**3 # ???
  12. w, h = image.shape
  13. # Fourier transform, normalize, remove DC bias
  14. data = np.fft.irfft(image, h*2, axis=1).reshape((w*h*2))
  15. data -= np.average(data)
  16. data *= (2**15.-1)/np.amax(data)
  17. data = array("h", np.int_(data)).tostring()
  18. # Write to disk
  19. output_file = wave.open(image_filename+".wav", "w")
  20. output_file.setparams((1, 2, 44100, 0, "NONE", "not compressed"))
  21. output_file.writeframes(data)
  22. output_file.close()
  23. print "Wrote %s.wav" % image_filename
  24. if __name__ == "__main__":
  25. import sys
  26. make_wav(sys.argv[1])